Home > Backend Development > PHP Tutorial > Detailed explanation of the difference between PHP isset() and empty()

Detailed explanation of the difference between PHP isset() and empty()

巴扎黑
Release: 2016-11-21 15:03:47
Original
1203 people have browsed it

PHP’s isset() function is generally used to detect whether a variable is set.

Format: bool isset (mixed var [, mixed var [, ...]])
Function: detect whether a variable is set.
Return value:
If the variable is not set If it exists, it returns 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, TRUE will be returned only when each single item meets the previous requirement , otherwise the result is FALSE
Versions: PHP 3, PHP 4, PHP 5
More instructions:
After using unset() to release a variable, it will no longer be isset().
PHP function isset() can only be used for variables, passing any other parameters will cause a parsing error.
To detect whether a constant has been set, use the defined() function.

PHP’s empty() function determines whether the value is empty

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 Its value is "", 0, "0", NULL,, FALSE, array(), var $var; and objects without any attributes, then TRUE is returned
If the variable exists and the value is not "", 0, "0 ", NULL,, FALSE, array(), var $var; and objects without any attributes, return FALSE
Versions: PHP 3, PHP 4, PHP 5
More instructions:
empty() return value =! (boolean) var, but no warning message will be generated because the variable is undefined. See Converting to Boolean for more information.
empty() can only be used for variables. Passing any other parameters will cause a Paser error and terminate the operation.
To detect whether a constant has been set, use the defined() function.
Example: A simple comparison between empty() and isset()

Php code

<?php   
$var = 0;   
// 结果为 true,因为 $var 为空   
if (emptyempty($var)) {   
echo &#39;$var is either 0 or not set at all&#39;;   
}   
// 结果为 false,因为 $var 已设置   
if (!isset($var)) {   
echo &#39;$var is not set at all&#39;;   
}   
?>
Copy after login

To talk about their connection, the common point is that empty() and isset() are both variable processing functions, and their function is to judge variables Whether they have been configured or not, it is precisely because of their great similarity in the process of processing variables that they have insufficient understanding of their relationship. If you only consider the two functions empty() and isset() themselves, it will make people more confused. Look at it from another angle. The processing objects of empty() and isset() are nothing more than undefined variables, 0, and empty strings.
If the variable is 0, empty() will return TRUE, isset() will return TRUE;
If the variable is an empty string, empty() will return TRUE, isset() will return TRUE;
If the variable is not defined, Then empty() will return TRUE, isset() will return FLASE;

The explanation of empty() in the manual is as follows:
Description bool empty(mixed var)
If var is a non-empty or non-zero value, then empty() Return FALSE. In other words, "", 0, "0", NULL, FALSE, array(), var $var; and objects without any properties will be considered empty, and TRUE will be returned if var is empty.
The explanation of isset() in the manual is as follows:
isset() detects whether the variable is set
Description bool isset (mixed var [, mixed var [, ...]] )
Returns TRUE if var exists, otherwise returns 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 ("?") is not equivalent to PHP's NULL constant.
Warning: isset() can only be used with variables, as 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.

When you want to judge 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 is not empty, you can use the empty function
When you want to judge whether a variable exists and is not empty, use the isset function first and then empty function


source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template