Home >Backend Development >PHP Problem >What does empty mean in php
In PHP, empty means "empty". It is a built-in function used to check whether a variable is empty. The syntax is "empty($var)"; when a variable value is 0, the empty character String, "0.0", ""0"", NULL, FALSE, empty array, empty() considers this variable to be equal to empty.
The operating environment of this tutorial: windows7 system, PHP7.1 version, DELL G3 computer
empty means "empty".
empty() is a built-in function in PHP that determines whether a variable is "empty".
empty will also detect whether the variable is empty or zero. When a variable value is 0, empty() considers the variable to be equivalent to being empty, which is equivalent to not being set.
Example:
$id=0; empty($id)?print "It's empty .":print "It's $id ."; //结果:It's empty . print "<br>"; !isset($id)?print "It's empty .":print "It's $id ."; //结果:It's 0 .
When a variable does not exist, or its value is equal to FALSE, then it is considered not to exist. empty() does not generate a warning if the variable does not exist.
Note: Prior to PHP 5.5, empty() only supported variables; anything else would cause a parsing error. In other words, the following code will not work:
empty(trim($name))
Instead, you should use:
trim($name) == false
empty() and will not generate a warning, even if the variable does not exist. This means that empty() is essentially equivalent to !isset($var) || $var == false.
Return value:
Returns FALSE when var exists and is a non-empty and non-zero value, otherwise returns TRUE.
The following variables will be considered empty:
""
(empty string)
0
(0 as an integer)
0.0
(0 as a floating point number)
"0"
(0 as a string)
NULL
FALSE
array()
(an empty array)
$ var;
(a variable declared but without a value)
Example:
<?php $ivar1=0; $istr1='Runoob'; if (empty($ivar1)) { echo '$ivar1' . " 为空或为 0。" . PHP_EOL; } else { echo '$ivar1' . " 不为空或不为 0。" . PHP_EOL; } if (empty($istr1)) { echo '$istr1' . " 为空或为 0。" . PHP_EOL; } else { echo '$istr1' . " 字符串不为空或不为0。" . PHP_EOL; } ?>
Output:
$ivar1 为空或为 0。 $istr1 字符串不为空或不为0。
Recommended Study: "PHP Video Tutorial"
The above is the detailed content of What does empty mean in php. For more information, please follow other related articles on the PHP Chinese website!