note 13122 deleted from language.types by jimw
| From: | jimw@php.net | Date: | Wed, 31 Oct 2001 01:47:24 +0000 |
| Subject: | note 13122 deleted from language.types by jimw | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-19682@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;
or better
settype($bla,'int');
is in this context correct and cannot be misunderstood.