以下功能让我发疯。究竟 100x 怎么可能等于 100,然后 100x 被报告为整数?
对于我的生活,我无法弄清楚。您可以复制并粘贴整个内容并亲自查看。
我在这里遗漏了一个简单的点,请帮帮我。
function blp_int($val) {
$orgval = $val;
$num = (int)$val;
echo "<li><font color=red>val: ". $val . " is being checked to see if it's an integer or not";
echo "<li><font color=red>orgval: ". $orgval ;
echo "<li><font color=red>num: ". $num ;
if ($orgval==$num) {
echo "<li><font color=red><b>YES IT IS! the [{$orgval}] is equal to [{$num}]</b>";
return true;
}
return false;
}
if (blp_int("100"))
{
echo "<h1>100 is an integer!</h1>";
}
else
{
echo "<h1>100 is NOT an integer!</h1>";
}
if (blp_int("100x"))
{
echo "<h1>100x is an integer!</h1>";
}
else
{
echo "<h1>100x is NOT an integer!</h1>";
}
上面的代码,运行时返回以下内容;
val: 100 is being checked to see if it's an integer or not
orgval: 100
num: 100
YES IT IS. the [100] is equal to [100]
100 is an integer!
val: 100x is being checked to see if it's an integer or not
orgval: 100x
num: 100
YES IT IS. the [100x] is equal to [100]
100x is an integer!
我可以通过添加以下位来解决这种情况
if (!is_numeric($val))
{
return false;
}
到 blp_int 函数的顶部,但是,.. 我仍然非常想知道为什么 php 认为 100x=100 是相等的。