php中empty⽅法,关于php的empty函数
最近在学习php的时候发现在php中进⾏判空操作的时候使⽤的是⼀个empty()函数。刚看的时候觉得这个⽅法很简单,就是判断变量对应类型的空值,后来在使⽤的时候发现⾃⼰想的太简单了,这个⽅法还是很有学问的,这⾥记录⼀下这个⽅法的详细⽤法,以及准备对⽐整理两个相似的⽅法ist()以及is_null()⽅法。
empty()函数
雅浦海沟
empty()⽅法的定义以及注释如下:/**
* Determine whether a variable is considered to be empty. A variable is considered empty if it does not exist or if its value
empty() does not generate a warning if the variable does not exist.
FALSE. empty()
* equals FALSE
* @param mixed $var
Variable to be checked.
*
empty() only supports variables; anything el will result in a par error. In other words,
Note: Prior to PHP 5.5, empty()
empty(trim($name)). Instead, u trim($name) == fal
trim($name) == fal.
* the following will not work: empty(trim($name))
*
*
* No warning is generated if the variable does not exist. That means empty()
empty() is esntially the conci equivalent
!
ist($var) || $var == fal.
* to !ist($var) || $var == fal
*
* @return bool
FALSE if var exists and has a non-empty, non-zero value. Otherwi returns TRUE
TRUE.
FALSE
*
* The following things are considered to be empty:
*
*
"" (an empty string)
*
0 (0 as an integer)
*
0.0 (0 as a float)
*
"0" (0 as a string)
*
NULL
*
FALSE
*
array() (an empty array)
*
$var; (a variable declared, but without a value)
*
*
*/
function PS_UNRESERVE_PREFIX_empty($var){};
其实注释已经说得很明⽩了,这⾥⼜要反思⾃⼰⾝上⼀个不好的习惯了,那就是不习惯看源码。现在的IDE点⼀下就可以直接跳转去看相应的源码,⽽⾃⼰还习惯于在⽹上查找,⽹上的讲解往往很零散,并且每个⼈在讲解的时候都加了⾃⼰的理解与看法,这样反⽽不利于⾃⼰的理解。
好了说了这么多废话,继续回到empty()本⾝,什么时候⼀个变量会被判断为‘empty’,也就是empty()
会返回true呢?如果⼀个变量不存在或者这个变量的值等于其类型零值的话,empty()函数就会返回true!。注释中还列出了具体的会被判断为‘empty’的所有情况,可以分为两类:变量不存在或者变量的值为类型零值,即:
(⼀)变量不存在
包括两种情况:⼀个变量不存在,即这个变量重来没有被声明过就直接塞给empty函数了;
⼀个变量只是被声明但是还没有被赋值;// 变量不存在
empty($var1); // return true
// 变量只声明没赋值
$var2;
中国孝道empty($var2); // return true
(⼆)变量值为零值
即每种数据类型对应的零值,具体为:int: 0
boolean: fal
梦见自己拉稀float: 0.0
string: “” and “0” ;
污水池
array: array()
object: null
字符串类型稍特殊⼀点,”0”也会被判断为empty; 但是两个或者以上的全‘0’字符串⼜会判断为⾮空,暂时还没有理解到这样规定的意义所在。
另外php的版本也会对empty()函数产⽣影响,注释⾥⾯说php5.5之前empty()⽅法只能检验变量,对常量使⽤的话会产⽣错误,⽽在
php5.5之后也可以检验常量,这点等换了电脑之后再验证。
ist()函数
在上⾯的注释中有这样⼀句话:
No warning is generated if the variable does not exist. That means empty() is esntially the conci equivalent to
!ist($var) || $var == fal.
意思就是说 empty() 等价于 !ist($var) || $var == fal
先看⼀下函数的定义以及注释:/**
*
Determine if a variable is t and is not NULL
NULL.
*
FALSE if testing a variable
ist() will return FALSE
If a variable has been unt with unt(), it will no longer be t. ist()
NULL constant.
* that has been t to NULL
NULL. Also note that a null character (" ") is not equivalent to the PHP NULL
*
TRUE only if all of the parameters are t.
ist() will return TRUE
If multiple parameters are supplied then ist()
* Evaluation goes from left to right and stops as soon as an unt variable is encountered.
* @param mixed $var
The variable to be checked.
* @param mixed $_ [optional]
Another variable ...
NULL, FALSE
FALSE otherwi.
* @return bool Returns TRUE
TRUE if var exists and has value other than NULL
生物题*/
function PS_UNRESERVE_PREFIX_ist($var, ...$_){};
注释说的很明⽩了,ist()⽤来检测⼀个变量是否已经声明并且其值不为NULL。有以下⼏种情况:若变量不存在,返回 FALSE
若变量存在并且值为NULL,返回 FALSE
若变量存在且值不为NULL,返回 TUREist($var1); //return fal
$var2 = null;
ist($var2); //return fal
$var3 = 0; // 赋值过
ist($var3); // return true
unt($var4); // 释放变量
ist($var4); // return fal
20年并且ist()⽀持同时检查多个变量,只有当每⼀个变量都符合上述返回true的条件的时候,整个⽅法才返回TRUE,也就是⼀个&&的关系。
天津社保这⾥要强调的⼀点就是ist()只适⽤于变量,⽤来检验常量的会报错。要检验常量可以使⽤defined()⽅法。
is_null()函数
从函数意义上is_null()函数就是ist()⽅法的反函数,对⽐理解就⾏了。
从⽤法上is_null()函数只能作⽤于已经声明的变量,对未声明的变量使⽤会报错的,这和ist()⽅法有
差别。/**
* Finds whether a variable is &null;
* @param mixed $var
* The variable being evaluated.
*
* @return bool true if var is null, fal
* otherwi.
* @since 4.0.4
* @since 5.0
青岛红岛*/
function is_null ($var) {}