note 37194 deleted from function.intval by didou
| From: | didou@php.net | Date: | Thu, 06 Nov 2003 14:01:54 +0000 |
| Subject: | note 37194 deleted from function.intval by didou | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-59990@lists.php.net to get a copy of this message | ||
Note Submitter: phpContrib (A T) esurfers d o t c o m
----
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...]