Home  >  Article  >  Backend Development  >  请教在if语句中 ! 和empty 是不是相同的

请教在if语句中 ! 和empty 是不是相同的

WBOY
WBOYOriginal
2016-06-13 12:14:501345browse

请问在if语句中 ! 和empty 是不是相同的;

$a=array();  $a=''; $a="0"; $a=null; ...
if(empty($a))   ==   if(!$a) ; 

最近看thinkphp框架代码, 在判断语句中,使用了好多empty()而不用 ! ;

求问:  框架编写者是基于什么目的 用empty()而不用! 

难道是 empty 比 ! 节省服务器资源?   很费解;    我相信肯定有原因的,求问原因;




 
 

------解决思路----------------------
我想你弄错了 ! 是非的意思   。
------解决思路----------------------
if(empty($a)) 判断$a是否为空,为空返回true,否则返回false
if(!$a)  $a如果存在返回false,否返回true

! 逻辑运算符,是取反的意思。
empty是方法。

两者区别在于,如果$a没有定义。empty不会出错。而!$a会出错。

<br />var_dump(empty($a));<br />var_dump(!$a);  // 会提示 Notice: Undefined variable: a<br />


Statement:
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