The content of this article is about the analysis and simple comparison of the isset() function and empty() function in PHP. It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you. Helps.
PHP isset function function
The isset function is to detect whether the variable is set.
Format: bool isset( mixed var [, mixed var [, ...]] )
Return value:
If the variable does not exist, return FALSE
If the variable exists and its value is NULL, it also returns FALSE
If the variable exists and its value is not NULL, it returns TRUE
When checking multiple variables at the same time, each single item is TRUE is returned only if the last requirement of the symbol is met, otherwise the result is FALSE
If a variable has been released using unset(), it will no longer be isset(). If you use isset() to test a variable that is set to NULL, it will return FALSE. Also note that a NULL byte ("\0") is not equivalent to PHP's NULL constant.
Warning: isset() can only be used with variables, because passing any other parameters will cause a parsing error. If you want to check whether a constant has been set, you can use the defined() function.
$a = array ('test' => 1, 'hello' => NULL); var_dump( isset ($a['test') ); // TRUE var_dump( isset ($a['foo') ); // FALSE var_dump( isset ($a['hello') ); // FALSE // 'hello' 等于 NULL,所以被认为是未赋值的。 // 如果想检测 NULL 键值,可以试试下边的方法。 var_dump( array_key_exists('hello', $a) ); // TRUE
PHP's empty() function
Format: bool empty(mixed var)
Function: Check whether a variable is empty
Return value:
If the variable does not exist, return TRUE
If the variable exists and the value is "", 0, "0", NULL,, FALSE, array(), var $var; and objects without any attributes, return TURE
If the variable exists and the value is not "", 0, "0", NULL,, FALSE, array(), var $var; and none If it is an object with any attribute, it will return FALSE
empty() can only be used for variables. Passing any other parameters will cause a Paser error and terminate the operation.
To check whether the constant has been set, you can use defined( )function.
A simple comparison between empty() and isset()
$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'; }
When you want to determine whether a variable has been declared, you can use the isset function
When you want to judge whether a variable has been assigned data and the filling position is empty, you can use the empty function
When you want to judge whether a variable exists and is not empty, first use the isset function, and then use the empty function
Related Article recommendation:
How to use PHP to obtain the visitor’s IP address (code)
Parsing of AES encrypted files in PHP (with code)
Request codes for post mode and get mode in curl of php
The above is the detailed content of Analysis and simple comparison of the functions of isset() function and empty() function in php. For more information, please follow other related articles on the PHP Chinese website!