转载:https://blog.csdn.net/qq_38812954/article/details/79581785php
判断变量的值,尤为是判断他们是否不为空,咱们有如下4种方法:数组
- if(isset($test)) true:变量已被赋值/设置
- if(!empty($test)) true:变量不为空
- if(!is_null($test)) true:变量不为空
- if($test) true:以自身为参数,变量不为空
(为方便讨论,empty与is_null均取反值,使4个函数都为true时,变量不为空)函数
四个函数的区别,先说结论0,例子具体分析看第1部分。测试
0.总结isset(), !empty(), !is_null(),以自身为参数的区别
- isset()、!empty()会首先检查变量是否存在(存在返回true),而后再对变量值进行检测;
is_null()、以自身为参数,直接检查变量值是否为null,若是变量未定义会出现错误警告。
- isset()、!empty()的输入参数必须是一个变量($变量),由于它们是语言结构,不是函数,没法被变量函数调用(参考阅读:可变函数);
is_null()、以自身为参数,输入参数只要是可以有返回值的就能够(常量、变量、表达式等均可以);
- 判断为空的时刻:
- isset():仅当 未定义 或者 值为null 时,返回false;
- !empty():未定义、 NULL、 “”(空字符)、0、“0”、FALSE、array(),均返回false;
- !is_null():直接判断是否不为null,只有为null才返回false;未定义会出现错误警告;
- 以自身为参数:未定义、 NULL、 “”(空字符)、0、“0”、FALSE、array(),均返回false;变量未定义时出现错误警告;
1.例子具体分析
4个函数对输入值为:数值(正常)、“”(空字符串)、array()(空数组)、0、“0”、false、null、值未定义,8种状况分别进行检验。
测试代码以下:spa
-
-
$test=array("数值"=>100,"空字符串\"\""=>"","空数组array()"=>array(),"数值0"=>0,"字符\"0\""=>"0","false"=>false,"null"=>null);
-
-
-
-
foreach( $test as $key=>$value){
-
echo 'try:$test',$i,'=',$key,'<br/>';
-
echo 'isset',isset($value)?' 1 define':' 0 undefine','<br/>';
-
echo '!empty',!empty($value)?' 1 no empty':' 0 empty','<br/>';
-
echo '!is_null',!is_null($value)?' 1 no null':' 0 null','<br/>';
-
echo '以自身为参数',$value?' 1 no null':' 0 null','<br/>';
-
-
-
-
-
-
-
-
echo 'try:$test',$i,'=',$key,'<br/>';
-
echo 'isset',isset($value)?' 1 define':' 0 undefine','<br/>';
-
echo '!empty',!empty($value)?' 1 no empty':' 0 empty','<br/>';
-
echo '!is_null',!is_null($value)?' 1 no null':' 0 null','<br/>';
-
echo '以自身为参数',$value?' 1 no null':' 0 null','<br/>';
-
-
-
测试结果以下:.net
try:$test1=数值
isset 1 define
!empty 1 no empty
!is_null 1 no null
以自身为参数 1 no nullcode
try:$test2=空字符串””
isset 1 define
!empty 0 empty
!is_null 1 no null
以自身为参数 0 nullblog
try:$test3=空数组array()
isset 1 define
!empty 0 empty
!is_null 1 no null
以自身为参数 0 null字符串
try:$test4=数值0
isset 1 define
!empty 0 empty
!is_null 1 no null
以自身为参数 0 nullget
try:$test5=字符”0”
isset 1 define
!empty 0 empty
!is_null 1 no null
以自身为参数 0 null
try:$test6=false
isset 1 define
!empty 0 empty
!is_null 1 no null
以自身为参数 0 null
try:$test7=null
isset 0 undefine
!empty 0 empty
!is_null 0 null
以自身为参数 0 null
try:$test8=值未定义
isset 0 undefine
!empty 0 empty
!is_null
Notice: Undefined variable: value in D:\xampp\htdocs\test\0105vs_isset_empty_is_null.php on line 22
0 null
以自身为参数
Notice: Undefined variable: value in D:\xampp\htdocs\test\0105vs_isset_empty_is_null.php on line 23
0 null
函数的true/false可用下表进行概括(”1”表true,”0”表false):
函数/$t的值 |
备注 |
isset($t) |
!empty($t) |
!is_null($t) |
$t |
100 |
有值 |
1 |
1 |
1 |
1 |
“” |
空字符串 |
1 |
0 |
1 |
0 |
array() |
空数组 |
1 |
0 |
1 |
0 |
0 |
数值0 |
1 |
0 |
1 |
0 |
“0” |
字符0 |
1 |
0 |
1 |
0 |
false |
false |
1 |
0 |
1 |
0 |
null |
null |
0 |
0 |
0 |
0 |
|
这里$t未定义 |
0 |
0 |
0(Notice) |
0(Notice) |
从上表可知:
- 对于值为null和未定义的变量,四种方式都能返回false
- 其中,!is_null()和“以自身为参数”对于未定义的变量还会出现Notice直接报错;
- !empty()和“以自身为参数” 还会对“”、array()、0、“0”、false,均返回false;
- 而isset()和!is_null()只对null和未定义变量作出false判断;
isset()、!empty()的输入参数必须是一个变量
-
-
echo isset($test),'<br/>';
-
echo !empty($test),'<br/>';
-
echo !is_null($test),!is_null(100),!is_null($test=100),'<br/>';