Home > Article > Backend Development > Detailed explanation of the difference between PHP isset() and empty(), issetempty_PHP tutorial
PHP’s isset() function is generally used to detect whether a variable is set
Format: bool isset ( mixed var [, mixed var [, ...]] )
Function: Detect whether the variable is set
Return value:
If the variable is not If it exists, return FALSE
If the variable exists and its value is NULL, it will also return FALSE
If the variable exists and its value is not NULL, it will return TRUE
When checking multiple variables at the same time, each single item meets the above Returns TRUE only if there is a request, otherwise the result is FALSE
Versions: PHP 3, PHP 4, PHP 5
More instructions:
After using unset() to release the variable, it will no longer be isset() .
The 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 a 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, return TURE
If the variable exists and the value is not "", 0, "0", NULL,, FALSE, array(), var $var; and none Object with any attribute, returns FALSE
Version: PHP 3, PHP 4, PHP 5
More explanation:
The return value of empty() =! (boolean) var, but it will not be because the variable is not Define a warning message. 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 of empty() and isset()
<?<span>php </span><span>$var</span> = 0<span>; </span><span>//</span><span> 结果为 true,因为 $var 为空 </span> <span>if</span> (<span>empty</span>(<span>$var</span><span>)) { </span><span>echo</span> '$var is either 0 or not set at all'<span>; } </span><span>//</span><span> 结果为 false,因为 $var 已设置 </span> <span>if</span> (!<span>isset</span>(<span>$var</span><span>)) { </span><span>echo</span> '$var is not set at all'<span>; } </span>?>
Note: Since this is a language structure rather than a function, it cannot be called by variable functions.
Note: empty() only detects variables, detecting 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 is a code of a detailed example of the isset and empty functions that has been tested. After reading this, it is basically the same:
<?<span>php </span><span>error_reporting</span>(<span>E_ALL</span><span>); </span><span>echo</span> '<B>未定义$var</b><Br>'<span>; </span><span>echo</span> "isset测试:<Br>"<span>; </span><span>if</span> ( <span>isset</span> ( <span>$var</span><span> )) { </span><span>echo</span> '变量$var存在!<Br>'<span> ; } </span><span>echo</span> "empty测试:<Br>"<span>; </span><span>if</span> ( <span>empty</span> ( <span>$var</span><span> )){ </span><span>echo</span> '变量$var的值为空<Br>'<span>; } </span><span>else</span><span> { </span><span>echo</span> '变量$var的值不为空<Br>'<span>; } </span><span>echo</span> "变量直接测试:<Br>"<span>; </span><span>if</span> ( <span>$var</span><span> ){ </span><span>echo</span> '变量$var存在!<Br>'<span>; } </span><span>else</span><span> { </span><span>echo</span> '变量$var不存在!<Br>'<span>; } </span><span>echo</span> '----------------------------------<br>'<span>; </span><span>echo</span> '<B>$var = \'\'</b><Br>'<span>; </span><span>echo</span> "isset测试:<Br>"<span>; </span><span>$var</span> = ''<span>; </span><span>if</span> ( <span>isset</span> ( <span>$var</span><span> )) { </span><span>echo</span> '变量$var存在!<Br>'<span> ; } </span><span>echo</span> "empty测试:<Br>"<span>; </span><span>if</span> ( <span>empty</span> ( <span>$var</span><span> )){ </span><span>echo</span> '变量$var的值为空<Br>'<span>; } </span><span>else</span><span> { </span><span>echo</span> '变量$var的值不为空<Br>'<span>; } </span><span>echo</span> "变量直接测试:<Br>"<span>; </span><span>if</span> ( <span>$var</span><span> ){ </span><span>echo</span> '变量$var存在!<Br>'<span>; } </span><span>else</span><span> { </span><span>echo</span> '变量$var不存在!<Br>'<span>; } </span><span>echo</span> '----------------------------------<br>'<span>; </span><span>echo</span> '<B>$var = 0</b><Br>'<span>; </span><span>echo</span> 'isset测试:<Br>'<span>; </span><span>$var</span> = 0<span> ; </span><span>if</span> ( <span>isset</span> ( <span>$var</span><span> )) { </span><span>echo</span> '变量$var存在!<Br>'<span> ; } </span><span>echo</span> "empty测试:<Br>"<span>; </span><span>if</span> ( <span>empty</span> ( <span>$var</span><span> )){ </span><span>echo</span> '变量$var的值为空<Br>'<span>; } </span><span>else</span><span> { </span><span>echo</span> '变量$var的值不为空<Br>'<span>; } </span><span>echo</span> "变量直接测试:<Br>"<span>; </span><span>if</span> ( <span>$var</span><span> ){ </span><span>echo</span> '变量$var存在!<Br>'<span>; } </span><span>else</span><span> { </span><span>echo</span> '变量$var不存在!<Br>'<span>; } </span><span>echo</span> '----------------------------------<br>'<span>; </span><span>echo</span> '<B>$var = null</b><Br>'<span>; </span><span>echo</span> 'isset测试:<Br>'<span>; </span><span>$var</span> = <span>null</span><span> ; </span><span>if</span> ( <span>isset</span> ( <span>$var</span><span> )) { </span><span>echo</span> '变量$var存在!<Br>'<span> ; } </span><span>echo</span> "empty测试:<Br>"<span>; </span><span>if</span> ( <span>empty</span> ( <span>$var</span><span> )){ </span><span>echo</span> '变量$var的值为空<Br>'<span>; } </span><span>else</span><span> { </span><span>echo</span> '变量$var的值不为空<Br>'<span>; } </span><span>echo</span> "变量直接测试:<Br>"<span>; </span><span>if</span> ( <span>$var</span><span> ){ </span><span>echo</span> '变量$var存在!<Br>'<span>; } </span><span>else</span><span> { </span><span>echo</span> '变量$var不存在!<Br>'<span>; } </span><span>echo</span> '----------------------------------<br>'<span>; </span><span>echo</span> '<B>$var ="php"</b><Br>'<span>; </span><span>echo</span> 'isset测试:<Br>'<span>; </span><span>$var</span> = "php"<span>; </span><span>if</span> ( <span>isset</span> ( <span>$var</span><span> )) { </span><span>echo</span> '变量$var存在!<Br>'<span> ; } </span><span>echo</span> "empty测试:<Br>"<span>; </span><span>if</span> ( <span>empty</span> ( <span>$var</span><span> )){ </span><span>echo</span> '变量$var的值为空<Br>'<span>; } </span><span>else</span><span> { </span><span>echo</span> '变量$var的值不为空<Br>'<span>; } </span><span>echo</span> "变量直接测试:<Br>"<span>; </span><span>if</span> ( <span>$var</span><span> ){ </span><span>echo</span> '变量$var存在!<Br>'<span>; } </span><span>else</span><span> { </span><span>echo</span> '变量$var不存在!<Br>'<span>; } </span>?>
When using PHP to write page programs, I often use variable processing functions to determine whether a variable value at the end of the PHP page is empty. At the beginning, I was used to using the empty() function, but I found some problems, so Use the isset() function instead and the problem is gone.
As the name suggests, empty() determines whether a variable is "empty", and isset() determines whether a variable has been set. It is this so-called "as the name implies" that made me take some detours at the beginning: when a variable value is equal to 0, empty() will also be true (True), so some accidents will occur. It turns out that although empty() and isset() are both variable processing functions, they are both used to determine whether the variable has been configured, but they have certain differences: empty will also detect whether the variable is empty or zero. When a variable value is 0, empty() considers the variable to be equivalent to being empty, which is equivalent to not being set.
For example, to detect the $id variable, when $id=0, use empty() and isset() to detect whether the variable $id has been configured. Both will return different values - empty() considers it not configured, isset () can get the value of $id:
<span>$id</span>=0<span>; </span><span>empty</span>(<span>$id</span>)?<span>print</span> "It's empty .":<span>print</span> "It's <span>$id</span> ."<span>; </span><span>//</span><span>结果:It's empty . </span> <span>print</span> "<br>"<span>; </span>!<span>isset</span>(<span>$id</span>)?<span>print</span> "It's empty .":<span>print</span> "It's <span>$id</span> ."<span>; </span><span>//</span><span>结果:It's 0 . </span>
This means that when we use variable processing functions, when the variable may have a value of 0, be careful when using empty(). It is more sensible to replace it with isset at this time.
When the URL tail parameter of a php page appears id=0 (for example: test.php?id=0), try to compare:
<span>if</span>(<span>empty</span>(<span>$id</span>)) <span>$id</span>=1; - 若 id=0<span> ,id 也会为1 </span><span>if</span>(!<span>isset</span>(<span>$id</span>)) <span>$id</span>=1; - 若 id=0 ,id 不会为1
The following code can be run separately to detect the above inference:
<span>if</span>(<span>empty</span>(<span>$id</span>)) <span>$id</span>=1<span>; </span><span>print</span> <span>$id</span>; <span>//</span><span> 得到 1 </span> <span>if</span>(!<span>isset</span>(<span>$id</span>)) <span>$id</span>=1<span>; </span><span>print</span> <span>$id</span>; <span>//</span><span>得到 0 </span>
要说它们的联系,其共同点就是empty()和 isset()都是变量处理函数,作用是判断变量是否已经配置,正是由于它们在处理变量过程中有很大的相似性,才导致对它们的关系认识不足。单从 empty()和isset()这两个函数本身来考虑的话会把人弄得更糊涂,换一个角度来它。empty()和isset()的处理对象无外乎未定义变量,0,空字符串。
如果变量为0,则empty()会返回TRUE,isset()会返回TRUE;
如果变量为空字符串,则empty()会返回TRUE,isset()会返回TRUE;
如果变量未定义,则empty()会返回TRUE,isset()会返回FLASE;
手册中对empty()的解释如下:
描述bool empty( mixed var )
如果 var 是非空或非零的值,则 empty() 返回 FALSE。换句话说,”"、0、”0″、NULL、FALSE、array()、var $var; 以及没有任何属性的对象都将被认为是空的,如果 var 为空,则返回 TRUE。
手册中对isset()的解释如下:
isset()检测变量是否设置
描述bool isset ( mixed var [, mixed var [, ...]] )
如果 var 存在则返回 TRUE,否则返回 FALSE。
如果已经使用 unset() 释放了一个变量之后,它将不再是 isset()。若使用 isset() 测试一个被设置成 NULL 的变量,将返回 FALSE。同时要注意的是一个 NULL 字节(”?”)并不等同于 PHP 的 NULL 常数。
警告: isset() 只能用于变量,因为传递任何其它参数都将造成解析错误。若想检测常量是否已设置,可使用 defined()函数。
当要 判断一个变量是否已经声明的时候 可以使用 isset 函数
当要 判断一个变量是否已经赋予数据且不为空 可以用 empty 函数
当要 判断 一个变量 存在且不为空 先isset 函数 再用 empty 函数