布尔值的 PHP IF 语句:$var === true vs $var

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1670138/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 03:27:18  来源:igfitidea点击:

PHP IF statement for Boolean values: $var === true vs $var

phpcoding-styleparadigms

提问by MarioRicalde

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?

以下哪个 IF 语句是最好和最快使用的?

<?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?

我知道 === 是精确匹配布尔值。然而,真的有任何改善吗?

回答by Gumbo

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 == trueis true but 1 === trueis false.

使用if ($var === true)orif ($var)不是风格问题,而是正确性问题。因为if ($var)if ($var == true). 并且==比较不检查类型。所以1 == true是真的但是1 === true是假的。

回答by dan_nl

As far as speed, I agree with Niels, it's probably negligible.

至于速度,我同意 Niels 的观点,它可能可以忽略不计。

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 可以具有的值。

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.

如果 $variable 使用 0 和 1 作为真/假标志,那么 if ( $variable ) 或 if ( !$variable ) 将起作用,但如果它是 strpos() 中的整数结果,您将遇到问题......如果可能,我建议使用实际的布尔值而不是 0 / 1。

... maybe this will help clarify; comment out the variations of $var to see the various results.

...也许这将有助于澄清;注释掉 $var 的变化以查看各种结果。

<?php

$var = true;
$var = 1;

$var = false;
$var = 0;

if ( $var ) {
    echo 'var = true <br />';
}

if ( $var === true ) {
    echo 'var is a boolean and = true';
}

if ( !$var ) {
    echo 'var = false <br />';
}

if ( $var === false ) {
    echo 'var is a boolean and = false';
}

回答by Anton Maryanov

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 == true) {} }'

time php -r '$r = true; for($i = 0; $i < 10000000; $i++) { if($r) {} }'

time php -r '$r = true; for($i = 0; $i < 10000000; $i++) { if($r) {} }'

The second one is faster than the first.

第二个比第一个快。

回答by Niels Bom

I'm not really deep into the technical stuff of PHP, but in the first case

我不是很深入了解 PHP 的技术内容,但在第一种情况下

if($variable === true)

$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.

$variable 必须具有与 true 完全相同的类型才能使 if 语句为 true。换句话说,$variable 不仅要解析为 true,还必须是一个布尔值。这是 2 个操作,值检查和类型检查。

In the second case

在第二种情况下

if($variable)

$variable only has to resolve to true. So only value checking occurs. I infer that that takes the computer a little less time.

$variable 只需要解析为 true。所以只发生值检查。我推断这会花费计算机更少的时间。

Practically speaking: the differences in speed are probably negligible.

实际上,速度的差异可能可以忽略不计。

回答by easement

=== 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.

=== 当第一根针在大海捞针的位置 0 时,在 strstr/stristr 中真的很有帮助。如果您不使用 === 或 !== ,您可能会遇到错误。

$str = "four score and...";
$position = strstr($str,'four');
if($position===FALSE) return FALSE;