Home > Article > Backend Development > Detailed explanation of php empty() function to check whether the variable is empty
empty() only detects variables, detecting anything that is not a variable will result in a parsing error. In other words, the following statements will have no effect: empty(addslashes($name))
empty — Checks whether a variable is empty
Report a bug Description
bool empty (mixed $var)
If var is a non-empty or non-zero value, empty() returns FALSE. In other words, "", 0, "0", NULL, FALSE, array(), var $var; and objects without any attributes will be considered empty , returns TRUE if var is empty.
In addition to not generating a warning when the variable is not set, empty() is the antonym of (boolean) var. See Converting to Boolean for more information.
Example #1 A simple comparison between empty() and isset().
The code is as follows:
<?php $var = 0; // 结果为 true,因为 $var 为空 if (empty($var)) { echo '$var is either 0 or not set at all'; } // 结果为 false,因为 $var 已设置 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 the variable function .
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 above is the detailed content of Detailed explanation of php empty() function to check whether the variable is empty. For more information, please follow other related articles on the PHP Chinese website!