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

发布于 2024-08-10 02:11:59 字数 238 浏览 6 评论 0原文

我知道这个问题并不是很重要..但是我一直想知道:

以下哪个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

爱殇璃 2024-08-17 02:11:59

使用 if ($var === true)if ($var) 不是风格问题,而是正确性问题。因为 if ($var)if ($var == true) 相同。并且 == 比较不会检查类型。因此 1 == true 为 true,但 1 === true 为 false。

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.

假面具 2024-08-17 02:11:59

就速度而言,我同意尼尔斯的观点,它可能可以忽略不计。

至于哪个 if 语句最适合测试,答案可能取决于预期的转换和 $variable 可以具有的值。

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

...也许这将有助于澄清;注释掉 $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';
}

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.

<?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';
}
要走干脆点 2024-08-17 02:11:59

只是一个事实:

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.

以往的大感动 2024-08-17 02:11:59

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

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

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

$str = "four score and...";
$position = strstr($str,'four');
if($position===FALSE) return FALSE;
回梦 2024-08-17 02:11:59

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

if($variable === true)

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

在第二种情况下,

if($variable)

$variable 只需解析为 true。所以只进行值检查。我推断这会减少计算机的时间。

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

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

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.

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.

Practically speaking: the differences in speed are probably negligible.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文