Home > Article > Backend Development > Judgment of parameter structure by empty and isset in PHP and the difference between empty() and isset(), emptyisset_PHP tutorial
No more nonsense, I will just post the code for you.
<?php class test{} $a1 = null; $a2 = ""; //$a3 = $a4 = 0; $a5 = '0'; $a6 = false; $a7 = array(); //var $a8; $a9 = new test(); for ($i=1; $i <=9 ; $i++) { $s = 'a'.$i; echo $i . ":"; var_dump(isset($$s)); echo '<br />'; } echo '<br />'; for ($i=1; $i <=9 ; $i++) { $s = 'a'.$i; echo $i . ":"; var_dump(empty($$s)); echo '<br />'; }
PS: The difference between empty() and isset() in PHP
For those who are new to PHP, it is difficult to figure out the difference between the usage of empty() and isset(). It is indeed difficult to figure out the difference in their usage without careful consideration.
Let’s talk about what they have in common:
can determine whether a variable is empty;
Both return boolean type, that is, true or false.
The following is a detailed description of the differences between their usage:
isset() is used to check whether a variable is set and can only be used for variables, because passing any other parameters will cause a parsing error. If you want to detect whether a constant has been set, use the defined() function. If a variable has been freed using unset(), it will no longer be isset(). If you use isset() to test a variable that is set to NULL, it will return FALSE. (Note the NULL byte ("
empty() is used to check whether a variable is empty.The biggest difference between them is the judgment of 0. If you use empty to judge, it will be considered to be empty, but if you use isset, it will be considered not to be empty. For example:
<?php var $a=0; //empty($a)返回true if(empty($a)){ echo "判断结果是空" } //isset($a)返回true if(isset($a)){ echo "判断结果不是空" } ?>