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

高洛峰
Release: 2023-03-05 08:40:02
Original
867 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 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, then Return TRUE
When checking multiple variables at the same time, TRUE will be returned only when each item meets the previous requirement, otherwise the result will be FALSE
Version: 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, return TURE
If the variable exists and the value is not "", 0, "0", NULL,, FALSE, array(), var $var; and objects without any attributes, then Return FALSE
Versions: PHP 3, PHP 4, PHP 5
More instructions:
The return value of empty() =! (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 
$var = 0; 
// 结果为 true,因为 $var 为空 
if (empty($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

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 detailed example code of isset and empty functions that has been tested by Script House. After reading this, it is basically the same:

<?php 
error_reporting(E_ALL); 
echo &#39;<B>未定义$var</b><Br>&#39;; 
echo "isset测试:<Br>"; 
if ( isset ( $var )) 
{ 
echo &#39;变量$var存在!<Br>&#39; ; 
} 
echo "empty测试:<Br>"; 
if ( empty ( $var )){ 
echo &#39;变量$var的值为空<Br>&#39;; 
} 
else 
{ 
echo &#39;变量$var的值不为空<Br>&#39;; 
} 
echo "变量直接测试:<Br>"; 
if ( $var ){ 
echo &#39;变量$var存在!<Br>&#39;; 
} 
else { 
echo &#39;变量$var不存在!<Br>&#39;; 
} 
echo &#39;----------------------------------<br>&#39;; 
echo &#39;<B>$var = \&#39;\&#39;</b><Br>&#39;; 
echo "isset测试:<Br>"; 
$var = &#39;&#39;; 
if ( isset ( $var )) 
{ 
echo &#39;变量$var存在!<Br>&#39; ; 
} 
echo "empty测试:<Br>"; 
if ( empty ( $var )){ 
echo &#39;变量$var的值为空<Br>&#39;; 
} 
else 
{ 
echo &#39;变量$var的值不为空<Br>&#39;; 
} 
echo "变量直接测试:<Br>"; 
if ( $var ){ 
echo &#39;变量$var存在!<Br>&#39;; 
} 
else { 
echo &#39;变量$var不存在!<Br>&#39;; 
} 
echo &#39;----------------------------------<br>&#39;; 
echo &#39;<B>$var = 0</b><Br>&#39;; 
echo &#39;isset测试:<Br>&#39;; 
$var = 0 ; 
if ( isset ( $var )) 
{ 
echo &#39;变量$var存在!<Br>&#39; ; 
} 
echo "empty测试:<Br>"; 
if ( empty ( $var )){ 
echo &#39;变量$var的值为空<Br>&#39;; 
} 
else 
{ 
echo &#39;变量$var的值不为空<Br>&#39;; 
} 
echo "变量直接测试:<Br>"; 
if ( $var ){ 
echo &#39;变量$var存在!<Br>&#39;; 
} 
else { 
echo &#39;变量$var不存在!<Br>&#39;; 
} 
echo &#39;----------------------------------<br>&#39;; 
echo &#39;<B>$var = null</b><Br>&#39;; 
echo &#39;isset测试:<Br>&#39;; 
$var = null ; 
if ( isset ( $var )) 
{ 
echo &#39;变量$var存在!<Br>&#39; ; 
} 
echo "empty测试:<Br>"; 
if ( empty ( $var )){ 
echo &#39;变量$var的值为空<Br>&#39;; 
} 
else 
{ 
echo &#39;变量$var的值不为空<Br>&#39;; 
} 
echo "变量直接测试:<Br>"; 
if ( $var ){ 
echo &#39;变量$var存在!<Br>&#39;; 
} 
else { 
echo &#39;变量$var不存在!<Br>&#39;; 
} 
echo &#39;----------------------------------<br>&#39;; 

echo &#39;<B>$var ="php"</b><Br>&#39;; 
echo &#39;isset测试:<Br>&#39;; 
$var = "php"; 
if ( isset ( $var )) 
{ 
echo &#39;变量$var存在!<Br>&#39; ; 
} 

echo "empty测试:<Br>"; 
if ( empty ( $var )){ 
echo &#39;变量$var的值为空<Br>&#39;; 
} 
else 
{ 
echo &#39;变量$var的值不为空<Br>&#39;; 
} 
echo "变量直接测试:<Br>"; 
if ( $var ){ 
echo &#39;变量$var存在!<Br>&#39;; 
} 
else { 
echo &#39;变量$var不存在!<Br>&#39;; 
} 
?>
Copy after login

When using PHP to write page programs, I often use variables The processing function determines whether a variable value at the end of the php page is empty. At first, I was used to using the empty() function, but I found some problems, so I switched to the isset() function and the problem no longer existed.
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 that there is no configuration, isset () can get the value of $id:

$id=0; 
empty($id)?print "It&#39;s empty .":print "It&#39;s $id ."; 
//结果:It&#39;s empty . 
print "<br>"; 
!isset($id)?print "It&#39;s empty .":print "It&#39;s $id ."; 
//结果:It&#39;s 0 .
Copy after login

This means that when we use the variable processing function, when the variable may have a value of 0, be careful when using empty() and replace it with isset at this time. Be smarter.
When the URL tail parameter of a php page appears id=0 (for example: test.php?id=0), try to compare:

if(empty($id)) $id=1; - 若 id=0 ,id 也会为1 
if(!isset($id)) $id=1; - 若 id=0 ,id 不会为1
Copy after login

You can run the following code separately to detect the above inference:

if(empty($id)) $id=1; 
print $id; // 得到 1 
if(!isset($id)) $id=1; 
print $id; //得到 0
Copy after login

要说它们的联系,其共同点就是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 函数

更多PHP isset()与empty()的使用区别详解相关文章请关注PHP中文网!

Related labels:
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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!