PHP null detection functions and methods summary

  • 2021-08-16 23:27:23
  • OfStack

Almost any incoming HTTP request will be checked for its parameters, and function 1 like isset () empty () is not uncommon.
The following test results are based on PHP 7.16

Whether to define judgment: isset ()

The isset () function can be used to determine whether a variable is defined and whether an element in the array is initialized. Its efficiency is about 4 times higher than that of array_key_exists ()


$a = 'test';
$b = array('key1' => 'value1');
$class = new stdClass();
var_dump(isset($class->test->test)); //  Determine whether the object attribute is defined: output  bool(false)
var_dump(isset($a)); //  Determine whether the variable is defined: output  bool(true)
var_dump(isset($c)); //  Determine whether the variable is defined: output  bool(false)
var_dump(isset($b['key1'])); //  Determine whether array elements are defined: output  bool(true)
var_dump(isset($b['key2'])); //  Determine whether array elements are defined: output  bool(false)

Whether it is empty or not: empty ()

The empty () function, which detects whether the variable is empty
Any 1 uninitialized quantity, 0, false, empty string, null, empty array, true will be returned when judged by empty


var_dump(empty($c));     //  Output  bool(true)
var_dump(empty($b['key2'])); //  Output  bool(true)
var_dump(empty($class));   //  Output  bool(false)
var_dump($class);       //  Output  object(stdClass)#1 (0) {}
var_dump(empty($class->test->test)); //  Output  bool(true)
class EmptyClass{};
$empty_class = new EmptyClass();
var_dump(empty($empty_class));//  Output  bool(false)

null = = $var Judgment

In addition to judging undefined variables, errors will be reported, and empty judges 1, 0, false, empty arrays, etc. will return true. In essence, = = expressions will automatically type conversion variables on both sides, so the judgment result is true.

is_null Judgment

When the single variable is assigned to null or NULL, the judgment result is true, and other cases are false. If the variable is not defined, an error will be reported

null = = = $var Judgment

Expression: = = = is type detection enforced. It not only detects the values of variables on both sides of the expression, but also detects the types of variables. true will be returned only if they are equal.

0 = = = $var: Check whether it is 0
false = = = $var: Detect for false
null = = = $var: Detect whether it is null
'' === $var: Detects whether it is an empty string and returns false if it contains any characters, such as space characters,\ 0
Using = = = makes a good distinction between empty strings, 0, false and null, and even between shaping and floating-point types.


$zero_int = 0;
$zero_bool = false;
$zero_double = 0.0;
$zero_null = null;
$zero_str = '';
var_dump(0 === $zero_int);   //  Output  bool(true)
var_dump(0 === $zero_bool);  //  Output  bool(false)
var_dump(0 === $zero_double); //  Output  bool(false)
var_dump(0 === $zero_null);  //  Output  bool(false)
var_dump(0 === $zero_str);   //  Output  bool(false)
var_dump('' === $zero_str);  //  Output  bool(true)
var_dump('' === $zero_int);  //  Output  bool(false)
var_dump('' === $zero_bool);  //  Output  bool(false)
var_dump(0.0 === $zero_double);//  Output  bool(true)
var_dump(0.0 === $zero_int);  //  Output  bool(false)


Related articles: