次の関数は私を夢中にさせました。いったいどうして 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 が等しいと考えるのかを知りたいと思っています。