note 13121 deleted from language.types by jeroen
| From: | jeroen@php.net | Date: | Sun, 28 Oct 2001 18:50:10 +0000 |
| Subject: | note 13121 deleted from language.types by jeroen | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-19533@lists.php.net to get a copy of this message | ||
If you want to find out if a number is a number and not a string:
--------------------------------------------------------------------------------
Sometimes you need to check, if the user has been really put in a number (e.g. in a form with very
strict validity checks or high security applications).
I saw checks like:
if ( is_int($bla) ) ...
This is WRONG, cause the user-input is ALWAYS handled as string. A better way is:
if ( $bla > 0 ) ...
This is also WRONG ins some cases, cause the user can send a string like "1askjhqwekljhg"
(yes, the integer value of this string is > 0!).
I found a better and more legal way; it is
if ( (int)$bla != (string)$bla ) ...
This works for PHP3 well, but not any more for PHP4. We need to extend it:
if ( (string)(int)$bla != (string)$bla ) ...
Confusing, but this works correct for all cases. A aftercoming
$bla=(int)$bla;
is in this case correct and cannot be misunderstood.