isset()
and ! The empty()
function is similar, both will return the same result. But the only difference is! The empty()
function does not generate any warnings or electronic notifications when the variable does not exist. It's enough to use either function. By merging two functions into a program it causes time lapse and unnecessary memory usage.
1.isset()
isset ( mixed $var , mixed $... = ? ) : bool
var: The variable to be checked.
Return value: If var exists and the value is not null, return true, otherwise return false.
PS: If a variable has been released using unset()
, it will no longer be isset(). If isset()
is used to test a variable that is set to null, false will be returned. At the same time, it should be noted that the null
character ("\0") is not equivalent to the null constant of PHP
. If multiple parameters are passed in at one time, then isset()
will only return when all parameters are set true
The calculation process is from left to right, and when an unset variable is encountered midway will stop immediately.
<?php $num = '0'; if( isset( $num ) ) { print_r(" $num is set with isset "); } echo "<br>"; // 声明一个空数组 $array = array(); echo isset($array['geeks']) ? 'array is set.' : 'array is not set.'; ?>
Output:
0 is set with isset functionarray is not set. array is not set.
##2.empty()
empty ( mixed $var ) : bool
false Otherwise return
true.
<?php $temp = 0; if (empty($temp)) { echo $temp . ' is considered empty'; } echo "\n"; $new = 1; if (!empty($new)) { echo $new . ' is considered set'; } ?>
0 is considered empty 1 is considered set
3. Similarities and differences between the two isset() and
! The empty() function is similar, both will return the same result. But the only difference is! The " "php tutorial" The above is the detailed content of Similarities and differences between isset() and !empty() functions in PHP. For more information, please follow other related articles on the PHP Chinese website!empty()
function does not generate any warnings or electronic notifications when the variable does not exist. It's enough to use either function. By merging two functions into a program it causes time lapse and unnecessary memory usage. <?php
$num = '0';
if( isset ( $num ) ) {
print_r( $num . " is set with isset function");
}
echo "\n";
$num = 1;
if( !empty ( $num ) ) {
print_r($num . " is set with !empty function");
}
?>
0 is set with isset function
1 is set with !empty function