PHP 布尔值 IF 语句:$var === true 与 $var
我知道这个问题并不是很重要..但是我一直想知道:
以下哪个 IF 语句使用起来最好且最快?
<?php
$variable = true;
if($variable === true)
{
//Something
}
if($variable)
{
// Something
}
?>
我知道 === 是完全匹配布尔值。然而,真的有改善吗?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
使用
if ($var === true)
或if ($var)
不是风格问题,而是正确性问题。因为if ($var)
与if ($var == true)
相同。并且==
比较不会检查类型。因此1 == true
为 true,但1 === true
为 false。Using
if ($var === true)
orif ($var)
is not a question of style but a question of correctness. Becauseif ($var)
is the same asif ($var == true)
. And==
comparison doesn’t check the type. So1 == true
is true but1 === true
is false.就速度而言,我同意尼尔斯的观点,它可能可以忽略不计。
至于哪个 if 语句最适合测试,答案可能取决于预期的转换和 $variable 可以具有的值。
如果 $variable 使用 0 和 1 作为真/假标志,那么 if ( $variable ) 或 if ( !$variable ) 会起作用,但如果它是 strpos() 中的整数结果,你会遇到问题......如果可能的话,我建议使用实际的布尔值而不是 0 / 1。
...也许这将有助于澄清;注释掉 $var 的变体以查看各种结果。
As far as speed, I agree with Niels, it's probably negligible.
As far as which if statement is best to test with, the answer probably depends on the expected casting and values $variable can have.
If $variable is using 0 and 1 as a true/false flag then if ( $variable ) or if ( !$variable ) would work, but if it's an integer result as in strpos() you'll run into problems ... if possible, I'd recommend using an actual boolean value rather than 0 / 1.
... maybe this will help clarify; comment out the variations of $var to see the various results.
只是一个事实:
time php -r '$r = true; for($i = 0; $i < 10000000; $i++) { if($r == true) {} }'
time php -r '$r = true; for($i = 0; $i < 10000000; $i++) { if($r) {} }'
第二个比第一个更快。
Just a fact:
time php -r '$r = true; for($i = 0; $i < 10000000; $i++) { if($r == true) {} }'
time php -r '$r = true; for($i = 0; $i < 10000000; $i++) { if($r) {} }'
The second one is faster than the first.
当第一根针位于大海捞针的位置 0 时, === 在 strstr/stristr 中确实很有帮助。如果您不使用 === 或 !== 您可能会遇到错误。
=== is really helpful in strstr/stristr when the first needle is in position 0 in the haystack. If you don't use === or !== you could have a bug on your hands.
我对 PHP 的技术内容并不是很深入,但在第一种情况下,
$variable 必须具有与 true 完全相同的类型,if 语句才为 true。换句话说,$variable 不仅必须解析为 true,而且还必须是布尔值。这就是两个操作,值检查和类型检查。
在第二种情况下,
$variable 只需解析为 true。所以只进行值检查。我推断这会减少计算机的时间。
实际上:速度差异可能可以忽略不计。
I'm not really deep into the technical stuff of PHP, but in the first case
$variable has to have the exact same type as true for the if statement to be true. In other words, $variable has not only to resolve to true, but also has to be a boolean. So that's 2 operations, value-checking and type-checking.
In the second case
$variable only has to resolve to true. So only value checking occurs. I infer that that takes the computer a little less time.
Practically speaking: the differences in speed are probably negligible.