Home >Backend Development >PHP Tutorial >php empty() checks whether a variable is empty_PHP tutorial

php empty() checks whether a variable is empty_PHP tutorial

WBOY
WBOYOriginal
2016-07-21 15:23:14903browse

empty — Check if a variable is empty

Report a bug Description

bool empty ( mixed $var )
If var is a non-empty or non-zero value, Then empty() returns FALSE. In other words, "", 0, "0", NULL, FALSE, array(), var $var; and objects without any properties will be considered empty, and TRUE is returned if var is empty.

empty() is the antonym of (boolean) var, except that it does not produce a warning when the variable is not set. See Converting to Boolean for more information.

Example #1 A simple comparison between empty() and isset().

Copy code The code is as follows:

$var = 0;
// Result is true because $var is empty
if (empty($var)) {
echo '$var is either 0 or not set at all';
}
// The result is false, Because $var is set
if (!isset($var)) {
echo '$var is not set at all';
}
?>

Note: Because it is a language constructor rather than a function, it cannot be called by variadic functions.

Note:

empty() only tests variables, testing anything that is not a variable will result in a parsing error. In other words, the following statement will not work: empty(addslashes($name)).

The following things are considered to be empty:

"" (an empty string)
0 (0 as an integer)
0.0 (0 as a float)
"0" (0 as a string)
NULL
FALSE
array() (an empty array)
var $var; (a variable declared, but without a value in a class)
Understanding of "empty array": array() (an empty array)

Copy code The code is as follows:

$array1=array();
print_r($array1);
if(empty($array1)){
echo 'Yes empty() is an empty array';
}
else{
echo 'For empty() is a noempty array';
}
?>
//Display results: #####################
Array
(
)
//For empty() it is an empty array
##############################
< ;?php
$array1=array();
$array1[]='';
print_r($array1);
if(empty($array1)){
echo ' An empty array for empty()';
}
else{
echo 'An empty array for empty()';
}
?>
//Display results: #####################
Array
(
[0] =>
)
//It is a noempty array for empty()
//################## ############
//This is not an empty array, because it has an element with an empty character (""), please pay attention to the empty character ("" (an empty string)) Difference;

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/324571.htmlTechArticleempty — Check whether a variable is empty Report a bug Description bool empty (mixed $var) If var is non-empty or non-zero value, empty() returns FALSE. In other words, "", 0, "0", ...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn