为什么使用 !== FALSE 检查 php 中的条带?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/699507/
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-24 23:33:34  来源:igfitidea点击:

Why use !== FALSE to check stripos in php?

phpcomparisonoperatorsconditional

提问by jergason

Here is the code I am looking at.

这是我正在查看的代码。

foreach ($header as $idx => $field) {
    if (stripos($field, 'foo') !== false) {
        $cols['foo'] = $idx;
    } else if (stripos($field, 'bar') !== false) {
        $cols['bar'] = $idx;
    } else if (stripos($field, 'brr') !== false) {
        $cols['brr'] = $idx;
    } else if (stripos($field, 'ffo') !== false) {
        $cols['ffo'] = $idx;
    }
}

Sorry, don't know how to format the code prettily either, any tips on that would be appreciated.

抱歉,也不知道如何漂亮地格式化代码,任何有关此的提示将不胜感激。

I am looking at some code written by someone much smarter than I, so I am not inclined to trust my first impression to just change everything to if(stripos($foo)), but why do it this way?

我正在查看由比我聪明得多的人编写的一些代码,所以我不倾向于相信我的第一印象只是将所有内容更改为if(stripos($foo)),但为什么要这样做呢?

回答by Cody Caughlan

The answer is that in PHP a "false" value can be satisfied by a handful of values, such as an empty array, an empty string, a NULL, integer 0, etc. See the empty() function page for a full list:

答案是,在 PHP 中,“false”值可以由少数值来满足,例如空数组、空字符串、NULL、整数 0 等。请参阅 empty() 函数页面以获取完整列表:

http://php.net/empty

http://php.net/空

So this would alwaysyield incorrect results:

所以这总是会产生不正确的结果:

if(strpos("abc", "a")) { 
  echo "Yes";
} else {
  echo "No";
}

Since the "a" occurs at the first position (index 0) then PHP considers "if (0)" to be false.

由于“a”出现在第一个位置(索引 0),因此 PHP 认为“if (0)”为假。

When strpos does NOT find the needle in your haystack it will return the boolean FALSE, which is what you want to check with the triple-equal operator which checks both type and value. See the docs on comparison operators

当 strpos 在您的 haystack 中没有找到指针时,它将返回布尔值 FALSE,这是您想要使用三重等号运算符检查的内容,该运算符检查类型和值。请参阅有关比较运算符的文档

http://www.php.net/manual/en/language.operators.comparison.php

http://www.php.net/manual/en/language.operators.comparison.php

回答by CMS

striposreturns the position of a string inside another, and if the string is not found, it returns false, so it's recommended to use the identity comparison operators(===, !==), because PHP considers 0 as a "falsy" value, consider this example:

stripos返回一个字符串在另一个字符串中的位置,如果没有找到,则返回false,所以建议使用恒等比较运算符(===, !==),因为PHP认为0是“falsy”价值,考虑这个例子:

// Find the position of the 'a' character in the 'abc' string:
stripos('abc', 'a') !== false; // true, position is 0
stripos('abc', 'a') != false; // false, 0 is "falsy"

回答by karim79

In PHP, !== means not of the same type AND value.

在 PHP 中,!== 表示不同类型的 AND 值。

If stripos() returns anything other than false (and exactly 'false', and not zero), it means it has found something, even if the position is 0 and int(0) is returned. 0 and false are equal when doing a standard comparison with the ==but not when using the identity ===operator, so the only way to know if stripos() has found something for certain is to compare false's value and type using !== (not identical, ie of both the same type and value of strpos()'s return value.)

如果 stripos() 返回 false 以外的任何值(并且恰好是“false”,而不是零),则意味着它找到了一些东西,即使位置为 0 并且返回了 int(0)。0 和 false在使用==进行标准比较时是相等的,但在使用身份===运算符时则不同,因此知道 stripos() 是否找到某些确定的东西的唯一方法是使用比较false的值和类型!==(不完全相同,即 strpos() 的返回值的类型和值都相同。)

回答by matpie

Because if the string matched at position 0, 0 evaluates to false, so you make sure to add the extra '=' so that type is taken in to consideration.

因为如果字符串在位置 0 匹配,则 0 的计算结果为 false,所以您一定要添加额外的 '=' 以便考虑该类型。

strpos()returns false when NOmatch is found.

strpos()返回false时,NO找到匹配。

Check out comparison operators.

查看比较运算符

回答by OIS

Dunno if something like this would be easier to maintain. Depends if you do something else in those if conditions. But you can set the keys in a config file, db, pass as argument to your function, or similar.

不知道这样的东西是否更容易维护。取决于你是否在那些 if 条件下做其他事情。但是您可以在配置文件 db 中设置键,作为参数传递给您的函数,或类似的。

$keys = array(
    'foo',
    'bar',
    'brr',
    'ffo',
);
foreach ($header as $idx => $field) {
    foreach ($keys as $key) {
        if (stripos($field, $key) !== false) {
            $cols[$key] = $idx;
            break;
        }
    }
}