PHP中的empty(),isset()和is_null()函数有什么区别?
我做了很多研究,但无法找到这三个之间的区别.因此,我做了一个简短的例子,希望对我们有所帮助.
I did lot of research but was unable to find the difference between these three. So I have done a short example I hope that will we helpful.
这是这三个表格的表述
Case Empty() isset() is_null()
1. $a=NULL 1 0 1
2. Not exists 1 0 1/Warning
3. $a='' 1 1 0
4. $a='NULL' 0 1 0
5. $a='testing' 0 1 0
这是实现它的代码.
PHP具有可用于测试变量值的不同功能.为此,三个有用的函数是 isset()
, empty()
和 is_null()
.所有这些函数都返回一个布尔值.如果未正确使用这些功能,可能会导致意外结果.
PHP has different functions which can be used to test the value of a variable. Three useful functions for this are isset()
, empty()
and is_null()
. All these function return a boolean value. If these functions are not used in correct way they can cause unexpected results.
isset()
和 empty()
通常被视为相反的函数,但这并不总是正确的.
isset()
and empty()
are often viewed as functions that are opposite, however this is not always true.
isset
— 确定是否设置了变量并且该变量不为NULL
换句话说,仅当变量不为null时,它才返回true.
In other words, it returns true only when the variable is not null.
空
-确定变量是否为空
换句话说,如果变量为空字符串,false,array(),NULL,"0",0和未设置的变量,则它将返回true.
In other words, it will return true if the variable is an empty string, false, array(), NULL, "0?, 0, and an unset variable.
is_null
— 查找变量是否为NULL
换句话说,仅当变量为null时,它才返回true.is_null()与isset()相反,不同之处在于isset()可以应用于未知变量,但is_null()仅适用于声明的变量
In other words, it returns true only when the variable is null. is_null() is opposite of isset(), except for one difference that isset() can be applied to unknown variables, but is_null() only to declared variables