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

WBOY
Release: 2016-06-23 13:40:40
Original
866 people have browsed it


$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会出错。

var_dump(empty($a));var_dump(!$a);  // 会提示 Notice: Undefined variable: a
Copy after login


source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!