gpt4 book ai didi

php - 为什么我的不等式测试失败了?

转载 作者:行者123 更新时间:2023-12-02 07:09:07 24 4
gpt4 key购买 nike

一向不稳定的 Xdebug 最近出现故障(将尽快恢复正常服务),所以我只好“通过 echo 调试”。

echo($path_info['filename'] . '    ' . $licence['issue_timestamp'].'<br>');      
if ($path_info['filename'] != $licence['issue_timestamp'])
{
die('They are NOT equal');
$_SESSION['error_messages'][] = 'This licence file has been copied';
return False;
}
else
die('They are equal');

输出

1319266557_   1319266557
They are equal

知道我做错了什么吗? (尾随)下划线有什么特别之处吗?

最佳答案

docs状态:

$a != $b Not equal TRUE if $a is not equal to $b after type juggling.

而且这种“打字杂耍”看起来非常可疑。在该页面的其他地方,它提到:

If you compare a number with a string or the comparison involves numerical strings, then each string is converted to a number and the comparison performed numerically. These rules also apply to the switch statement. The type conversion does not take place when the comparison is === or !== as this involves comparing the type as well as the value.

这个例子是关于你的案例中发生的事情的一个死的赠品:

var_dump(100 == "1e2"); // 100 == 100 -> true

关于字符串如何转换为整数,可见here .突出的一点是(我的粗体):

If the string does not contain any of the characters '.', 'e', or 'E' and the numeric value fits into integer type limits (as defined by PHP_INT_MAX), the string will be evaluated as an integer. In all other cases it will be evaluated as a float.

The value is given by the initial portion of the string.

Example: $foo = 1 + "10 Small Pigs"; // $foo is integer (11)

这就是为什么“123_”等于“123”(a)。最重要的是,使用 !== 因为这样可以确保值和类型相同。


(a) 查看 online PHP executor : enter image description here

关于php - 为什么我的不等式测试失败了?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7873147/

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