Home >Backend Development >PHP Problem >PHP determines that it is not empty
empty() function: detect whether the variable is "empty"
Description: Any uninitialized variable, the value is 0 or false Or empty string "" or null variable, empty array, object without any attributes, empty(variable) == true. (Recommended learning: PHP video tutorial)
Note 1: Uninitialized variables can also be detected as "empty" by empty.
Note 2: empty can only detect variables, not statements.
<?php $a = 0; $b = ''; $c = array(); if (empty($a)) echo '$a 为空' . "<br/>"; if (empty($b)) echo '$b 为空' . "<br/>"; if (empty($c)) echo '$c 为空' . "<br/>"; if (empty($d)) echo '$d 为空' . "<br/>"; //结果 // $a 为空 // $b 为空 // $c 为空 // $d 为空 var_dump(empty(null)); var_dump(empty(0)); var_dump(empty('')); var_dump(empty([])); //结果 // bool(true) // bool(true) // bool(true) // bool(true)
var == null Function: Determine whether the variable is "empty"
Description: A variable whose value is 0 or false or an empty string "" or null, Empty arrays will be judged as null.
Note: The significant difference from empty is that var == null will report an error when the variable is not initialized.
is_null Function: Detect whether the variable is "null"
Description: When the variable is assigned a value of "null", the detection result is true.
Note 1: null is not case-sensitive: a=null;a=null;a = NULL makes no difference.
Note 2: Only when the value of the variable is "null", the detection result is true. 0, empty string, false, and empty array are all detected as false.
Note 3: When the variable is not initialized, the program will report an error.
The above is the detailed content of PHP determines that it is not empty. For more information, please follow other related articles on the PHP Chinese website!