if(!empty($x))
和 if(@$x)
有什么功能上的区别吗?
注意:我知道@
可以抑制错误,我不会轻易使用它。
if(@$x)
可能没有功能上的区别,但这种写法似乎不太合适,而且有不使用它的理由。if(@$x)
可能看不到任何区别,但背后还有一些额外的工作。这是因为无论如何都会调用错误处理程序,即使您抑制了错误。When $x="foo";
@$x == true
!empty($x) == true
isset($x) == true
When $x has not been set!
@$x == false
!empty($x) == false
isset($x) == false
这是另一个具有不同x值的集合。
When $x=0;
@$x == false
!empty($x) == false
isset($x) == true
再来一次,这次是1。
When $x=1;
@$x == true
!empty($x) == true
isset($x) == true
var_dump(!empty($x));
var_dump(!!@$x);
$x is an empty array
boolean false
boolean false
$x is int(1)
boolean true
boolean true
$x is int(0)
boolean false
boolean false
$x is float(0.1)
boolean true
boolean true
$x is string(0)
boolean false
boolean false
$x is string(1)
boolean true
boolean true
$x is string(abc)
boolean true
boolean true
$x is instance of stdClass
boolean true
boolean true
$x is true
boolean true
boolean true
$x is false
boolean false
boolean false
$x is defined null
boolean false
boolean false
$x is not set
boolean false
boolean false
if(@$x)
这种用法。它常见吗? - TecBrat!
。此外,请参阅type comparison tables。 - Gumbo