Home >Backend Development >PHP Tutorial >Detailed explanation of the difference between isset and empty in php
1. 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 both determine whether a variable is empty;
both return boolean Type, i.e. true or false.
Let’s talk about the difference between their usage in detail:
isset() is used to detect whether the variable is set, and can only be used for variables , because passing any other parameters will cause a parsing error. If you want to check 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 that a NULL byte ("\0") is not equivalent to PHP's NULL constant)
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, and 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 "判断结果不是空" } ?>
Related recommendations:
Have questions about isset and empty?
The difference between isset and empty_PHP tutorial
Example study on the difference between PHP function isset and empty
The above is the detailed content of Detailed explanation of the difference between isset and empty in php. For more information, please follow other related articles on the PHP Chinese website!