The difference between isset() and empty() in php is: isset() function is used to detect whether the variable is set, the syntax format is: [isset (mixed var)]; empty() function is used to determine the value Whether it is empty, the syntax format is: [empty(mixed var)].
isset() function
isset() function is generally used to detect whether a variable is set.
Format:
bool isset ( mixed var [, mixed var [, ...]] )
Return value:
If the variable does not exist, it returns FALSE;
If the variable exists and its value is NULL, it also returns FALSE ;
If the variable exists and the value is not NULL, TRUE is returned;
When multiple variables are checked at the same time, TRUE is returned only when each single item meets the previous requirement, otherwise the result is FALSE .
empty() function
empty() function is used to determine whether the value is empty.
Format:
bool empty ( mixed var )
Return value:
If the variable does not exist, it returns TRUE;
If the variable exists and its value is "", 0, "0", NULL,, FALSE, array(), var $var; and objects without any attributes, return TRUE;
If the variable exists and the value is not "", 0, "0", NULL, FALSE, array(), var $var; and objects without any attributes, return FALSE.
Example: Simple comparison of empty() and isset()
<?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: empty() only detects variables, and detecting anything that is not a variable will cause a parsing error.
If you want to learn more related knowledge, please visit php Chinese website.
The above is the detailed content of What is the difference between isset() and empty() in php. For more information, please follow other related articles on the PHP Chinese website!