note 37194 added to function.intval
| From: | phpContrib at rn2 dot php dot net | Date: | Thu, 06 Nov 2003 13:58:21 +0000 |
| Subject: | note 37194 added to function.intval | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-59989@lists.php.net to get a copy of this message | ||
String "+0134.000" is integer valued!!!
This function will work (full testing form included) even for such cases:
<?php
if(!$val) $val="+0134.000";
if(real_is_intValued($val)) echo('"'.$val.'" is INTEGER valued');
else echo('"'.$val.'" is not an integer');
echo('<form action="testint.php"><input name="val"
value="'.$val.'"><input type=submit></form>');
function real_is_intValued($var) {
if(is_numeric($var)) {
// It's numeric valued so it's at least a float
$var=(float)$var;
return ((float)(int)$var)===$var;
}
return FALSE;
}
?>
Functions that convert $var to int and then back to string will FAIL!!
Casting "03.0" to int will produce 3
Casting 3 to string will produce "3"
But "03.0" is not == "3".
Using strtoupper() is also not correct, because it will assume that ";,.#" is a number.
Also things like if($var+1 != 1 || $var == "0") {...} will fail if $var is
"0000"
[NOTE: this post has been previously removed by someone that left the NOT WORKING functions on
instead!! Test the code please...]
----
Manual Page -- http://www.php.net/manual/en/function.intval.php
Edit -- http://master.php.net/manage/user-notes.php?action=edit+37194
Delete -- http://master.php.net/manage/user-notes.php?action=delete+37194&report=yes
Reject -- http://master.php.net/manage/user-notes.php?action=reject+37194&report=yes
Search -- http://master.php.net/manage/user-notes.php