gpt4 book ai didi

php - 评估 stripos(),!== FALSE 和 === TRUE 之间有什么区别?

转载 作者:塔克拉玛干 更新时间:2023-11-03 06:05:41 24 4
gpt4 key购买 nike

我有一个字符串问题:

$val = 'NOT NULL';

if(stripos($val, 'NULL') !== FALSE){
echo "IS $val";
}

它的评估很好,但如果我使用 === TRUE 作为评估器,事情就会出错。答案让我望而却步,请帮助我理解。

最佳答案

如果您阅读 stripos() 的文档你会发现。

Returns the position of where the needle exists relative to the beginnning of the haystack string (independent of offset). Also note that string positions start at 0, and not 1.

Returns FALSE if the needle was not found.

它不返回 TRUE。由于您使用的是 strict equality ,您的条件将永远为真

如果您执行了 stripos($val, 'NULL') == TRUE 那么如果在位置 0 处找到了 NULL,您的代码就会执行> - 因为 PHP 会做一些类型转换并且有效地 0 == (int)true

使用 stripos() 测试存在性的适当方法就是您所拥有的:

if (stripos($val, 'NULL') !== FALSE){
echo "IS $val";
}

关于php - 评估 stripos(),!== FALSE 和 === TRUE 之间有什么区别?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18382466/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com