如何检查PHP中的值是否为空[JSON]

时间:2018-06-22 18:19:24

标签: php

我有一些JSON参数,我想使用IF语句验证该值是否为空。我在函数中编写了IF语句进行检查,但是IF语句仅检查参数,如果它们为空,我想检查这些值。拜托,我该怎么做。 我的代码

   //api.php

    $payment_type = $this->validateParameter('payment_type', $this->param['payment_type'], STRING, true);

$date_of_delivery = $this->validateParameter('date_of_delivery', $this->param['date_of_delivery'], STRING, true);

 $your_generated_waybill_number = $this->validateParameter('your_generated_waybill_number', $this->param['your_generated_waybill_number'], STRING, true);


   "payment_type" - is an example of parameter ||  "1" - is an example of value



     {
  "name":"create_insert_new_delivery",
  "param":{
       "payment_type":"1",
       "date_of_delivery":"", //E.g here want to check if the value is empty
       "your_generated_waybill_number":"39ei40909444567avaab",
       }
    }





  //rest.php
   public function validateParameter($fieldName, $value, $required = true){

    if($required == true && empty($value) == true){
        $this->throwError(EMPTY_PARAMETER, $fieldName . " parameter is missing"); //HERE check if the parameter is missing and fires error, but I also want to include value check
    } else if ($required == true && empty($fieldName) == true){
        $this->throwError(API_PARAM_REQUIRED, $fieldName . " value is required");
    } else {

    } //check when parameter is present but value is empty and leave if it is not required

  }

4 个答案:

答案 0 :(得分:3)

应该使用身份运算符来测试变量。

if (!$var){}

if  ($var === null){} //checks by type

if (empty($field)){}

答案 1 :(得分:2)

使用empty()函数检查值

$str = '';
// or 
$str = null;
// or 
$str = false;
var_dump(empty($str)) // output => true

要检查是否定义了变量,可以使用isset

http://php.net/manual/ro/function.isset.php

答案 2 :(得分:2)

由于正在执行的测试已经返回true或false,因此您可以使函数更清晰一些。无需将它们与真实值进行比较。

from io import StringIO

import matplotlib.pyplot as plt
import pandas as pd
import seaborn as sn

# This is what I wish I had.
csv_source1 = StringIO("""\
year,Apples,Bananas,Cherries
1990,1,2,3
1997,1,4,9
1999,1,8,27
""")
df1 = pd.read_csv(csv_source1, index_col=0)
df1.index.names = ['Year']
df1.columns.names = ['fruit']

# This is what I actually have.
csv_source2 = StringIO("""\
fruit,1990,1997,1999
Apples,1,1,1
Bananas,2,4,8
Cherries,3,9,27
""")
# So I convert the years to integers and transpose it.
df2 = pd.read_csv(csv_source2, index_col=0)
df2.columns = df2.columns.astype(int)
df2 = df2.T
df2.index.names = ['Year']

sn.set()
ax = plt.subplot(211)
df1.plot(ax=ax)

ax = plt.subplot(212)
df2.plot(ax=ax)

plt.tight_layout()
plt.show()

答案 3 :(得分:2)

主要问题是要将值(从数组中获取)传递给方法并进行检查。因为到那时,如果未设置数组键,则会触发警告,并将该值强制为null

empty():如果未设置变量或者其值为假值(例如falsenull[],{{1} }等)

"":仅在未设置值的情况下返回true。

所以:

isset()

因此您可以像这样编辑验证功能:

$a = ['one' => false];
empty($a['one']); //true
isset($a['one']); //true
isset($a['two']); //false

注意:由于您正在检查类属性public function validateParameter($fieldName, &$inputArray, $required = true){ if($required){ if(!isset($inputArray[$fieldName])){ $this->throwError(EMPTY_PARAMETER, $fieldName . " parameter is missing"); } else if (empty($inputArray[$fieldName])){ $this->throwError(API_PARAM_REQUIRED, $fieldName . " value is required"); } } else { if(!isset($inputArray[$fieldName])){ return null; } } return $inputArray[$fieldName]; } 。您可以避免将数组传递给方法,而只需在函数中引用$this->param,除非它应该更可重用。

我不完全了解您期望的结果,但做出了合理的假设