下面的函数使我疯狂。 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是等于。
'is_int','ctype_digit','filter_var($ string,FILTER_VALIDATE_INT)',如果需要,后者可以禁止/允许十六进制和八进制整数。如果你想使用你的代码,大小写为int,但是返回字符串:'if((string)$ orgval ==(string)(int)$ orgval)' – Wrikken 2012-02-29 19:38:36
你的函数使用'echo'和'return',尽量不要用这种方式写函数。输出是什么?我的意思是当在int转换之前和之后'回显变量时,你在屏幕上看到了什么? – 2012-02-29 19:39:39