Heim > Artikel > Backend-Entwicklung > PHP 判断数组是否为空的5大方法_PHP教程
1. isset功能:判断变量是否被初始化
说明:它并不会判断变量是否为空,并且可以用来判断数组中元素是否被定义过
注意:当使用isset来判断数组元素是否被初始化过时,它的效率比array_key_exists高4倍左右
<ol class="dp-c"><li class="alt"><span><span><?php </span></span></li><li><span><span class="vars">$a</span><span> = </span><span class="string">''</span><span>; </span></span></li><li class="alt"><span><span class="vars">$a</span><span>[</span><span class="string">'c'</span><span>] = </span><span class="string">''</span><span>; </span></span></li><li><span><span class="keyword">if</span><span> (!isset(</span><span class="vars">$a</span><span>)) </span><span class="func">echo</span><span> </span><span class="string">'$a 未被初始化'</span><span> . </span><span class="string">""</span><span>; </span></span></li><li class="alt"><span><span class="keyword">if</span><span> (!isset(</span><span class="vars">$b</span><span>)) </span><span class="func">echo</span><span> </span><span class="string">'$b 未被初始化'</span><span> . </span><span class="string">""</span><span>; </span></span></li><li><span><span class="keyword">if</span><span> (isset(</span><span class="vars">$a</span><span>[</span><span class="string">'c'</span><span>])) </span><span class="func">echo</span><span> </span><span class="string">'$a 已经被初始化'</span><span> . </span><span class="string">""</span><span>; </span></span></li><li class="alt"><span><span class="comment">// 显示结果为</span><span> </span></span></li><li><span><span class="comment">// $b 未被初始化</span><span> </span></span></li><li class="alt"><span><span class="comment">// $a 已经被初始化</span><span> </span></span></li></ol>
2. empty功能:检测变量是否为”空”
说明:任何一个未初始化的变量、值为 0 或 false 或 空字符串”” 或 null的变量、空数组、没有任何属性的对象,都将判断为empty==true
注意1:未初始化的变量也能被empty检测为”空”
注意2:empty只能检测变量,而不能检测语句
<ol class="dp-c"><li class="alt"><span><span><?php </span></span></li><li><span><span class="vars">$a</span><span> = 0; </span></span></li><li class="alt"><span><span class="vars">$b</span><span> = </span><span class="string">''</span><span>; </span></span></li><li><span><span class="vars">$c</span><span> = </span><span class="keyword">array</span><span>(); </span></span></li><li class="alt"><span><span class="keyword">if</span><span> (</span><span class="func">empty</span><span class="keyword">empty</span><span>(</span><span class="vars">$a</span><span>)) </span><span class="func">echo</span><span> </span><span class="string">'$a 为空'</span><span> . </span><span class="string">""</span><span>; </span></span></li><li><span><span class="keyword">if</span><span> (</span><span class="func">empty</span><span class="keyword">empty</span><span>(</span><span class="vars">$b</span><span>)) </span><span class="func">echo</span><span> </span><span class="string">'$b 为空'</span><span> . </span><span class="string">""</span><span>; </span></span></li><li class="alt"><span><span class="keyword">if</span><span> (</span><span class="func">empty</span><span class="keyword">empty</span><span>(</span><span class="vars">$c</span><span>)) </span><span class="func">echo</span><span> </span><span class="string">'$c 为空'</span><span> . </span><span class="string">""</span><span>; </span></span></li><li><span><span class="keyword">if</span><span> (</span><span class="func">empty</span><span class="keyword">empty</span><span>(</span><span class="vars">$d</span><span>)) </span><span class="func">echo</span><span> </span><span class="string">'$d 为空'</span><span> . </span><span class="string">""</span><span>; </span></span></li></ol>
3. var == null功能:判断变量是否为”空”
说明:值为 0 或 false 或 空字符串”” 或 null的变量、空数组、都将判断为 null
注意:与empty的显著不同就是:变量未初始化时 var == null 将会报错。
<ol class="dp-c"><li class="alt"><span><span><?php </span></span></li><li><span><span class="vars">$a</span><span> = 0; </span></span></li><li class="alt"><span><span class="vars">$b</span><span> = </span><span class="keyword">array</span><span>(); </span></span></li><li><span><span class="keyword">if</span><span> (</span><span class="vars">$a</span><span> == null) </span><span class="func">echo</span><span> </span><span class="string">'$a 为空'</span><span> . </span><span class="string">""</span><span>; </span></span></li><li class="alt"><span><span class="keyword">if</span><span> (</span><span class="vars">$b</span><span> == null) </span><span class="func">echo</span><span> </span><span class="string">'$b 为空'</span><span> . </span><span class="string">""</span><span>; </span></span></li><li><span><span class="keyword">if</span><span> (</span><span class="vars">$c</span><span> == null) </span><span class="func">echo</span><span> </span><span class="string">'$b 为空'</span><span> . </span><span class="string">""</span><span>; </span></span></li><li class="alt"><span><span class="comment">// 显示结果为</span><span> </span></span></li><li><span><span class="comment">// $a 为空</span><span> </span></span></li><li class="alt"><span><span class="comment">// $b 为空</span><span> </span></span></li><li><span><span class="comment">// Undefined variable: c</span><span> </span></span></li></ol>
4. is_null功能:检测变量是否为”null”
说明:当变量被赋值为”null”时,检测结果为true
注意1:null不区分大小写:$a = null; $a = NULL 没有任何区别
注意2:仅在变量的值为”null”时,检测结果才为true,0、空字符串、false、空数组都检测为false
注意3:变量未初始化时,程序将会报错
<ol class="dp-c"><li class="alt"><span><span><?php </span></span></li><li><span><span class="vars">$a</span><span> = null; </span></span></li><li class="alt"><span><span class="vars">$b</span><span> = false; </span></span></li><li><span><span class="keyword">if</span><span> (</span><span class="func">is_null</span><span>(</span><span class="vars">$a</span><span>)) </span><span class="func">echo</span><span> </span><span class="string">'$a 为NULL'</span><span> . </span><span class="string">""</span><span>; </span></span></li><li class="alt"><span><span class="keyword">if</span><span> (</span><span class="func">is_null</span><span>(</span><span class="vars">$b</span><span>)) </span><span class="func">echo</span><span> </span><span class="string">'$b 为NULL'</span><span> . </span><span class="string">""</span><span>; </span></span></li><li><span><span class="keyword">if</span><span> (</span><span class="func">is_null</span><span>(</span><span class="vars">$c</span><span>)) </span><span class="func">echo</span><span> </span><span class="string">'$c 为NULL'</span><span> . </span><span class="string">""</span><span>; </span></span></li><li class="alt"><span><span class="comment">// 显示结果为</span><span> </span></span></li><li><span><span class="comment">// $a 为NULL</span><span> </span></span></li><li class="alt"><span><span class="comment">// Undefined variable: c</span><span> </span></span></li></ol>
5. var === null功能:检测变量是否为”null”,同时变量的类型也必须是”null”
说明:当变量被赋值为”null”时,同时变量的类型也是”null”时,检测结果为true
注意1:在判断为”null”上,全等于和is_null的作用相同
注意2:变量未初始化时,程序将会报错
总结:
PHP中,”NULL” 和 “空” 是2个概念。
isset 主要用来判断变量是否被初始化过
empty 可以将值为 “假”、”空”、”0″、”NULL”、”未初始化” 的变量都判断为TRUE
is_null 仅把值为 “NULL” 的变量判断为TRUE
var == null 把值为 “假”、”空”、”0″、”NULL” 的变量都判断为TRUE
var === null 仅把值为 “NULL” 的变量判断为TRUE
注意:在判断一个变量是否真正为”NULL”时,大多使用 is_null,从而避免”false”、”0″等值的干扰。