PHP IF语句的布尔值:$ var === true vs $ var [英] PHP IF statement for Boolean values: $var === true vs $var
本文介绍了PHP IF语句的布尔值:$ var === true vs $ var的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我知道这个问题不是很重要..但我一直在想:
下面哪个IF语句是最好的和最快的使用? / p>
<?php
$ variable = true;
if($ variable === true)
{
//某事
}
if($ variable)
{
// somethinghing
}
?>
我知道===是完全匹配的布尔值。 c> if($ var === true)
使用 if($ var === true) code>或 if($ var)
不是风格的问题,而是正确性的问题。由于 if($ var)
与 if($ var == true)
相同。和 ==
比较不检查类型。因此 1 == true
为真,但 1 === true
为假。
I know this question is not really important.. however I've been wondering:
Which of the following IF statements is the best and fastest to use?
<?php
$variable = true;
if($variable === true)
{
//Something
}
if($variable)
{
// Something
}
?>
I know === is to match exactly the boolean value. However is there really any improvement?
解决方案 Using if ($var === true)
or if ($var)
is not a question of style but a question of correctness. Because if ($var)
is the same as if ($var == true)
. And ==
comparison doesn’t check the type. So 1 == true
is true but 1 === true
is false.
这篇关于PHP IF语句的布尔值:$ var === true vs $ var的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文