note 15551 deleted from function.is-numeric by googleguy
| From: | googleguy@php.net | Date: | Sat, 19 Jan 2013 03:49:27 +0000 |
| Subject: | note 15551 deleted from function.is-numeric by googleguy | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-193260@lists.php.net to get a copy of this message | ||
Note Submitter: aulbach at unter dot franken dot de
----
If you want to check, if a string will be converted into the exactly same number, you have to test
the following:
if ( (string)(int)$val === (string)$val ) ...
This is not the same as
if ( is_numeric($val) ) ...
cause
<?php
$a="0000001";
if ( is_numeric($a) ) echo "a is numeric";
if ( (string)(float)$a !== (string)$a) echo "a cannot be converted 100%-correctly";
?>
will output:
a is numeric
a cannot be converted 100%-correctly
Ok, ok, I know. This is a hysteric type-check. It depends on the type of application, if you use it.
The aim should always be, that the user could be warned, if the value cannot be stored in the way,
the user types it into.
Why? Therefore is the following example:
If you set
$a="1E+17";
the above script says that it is all right.
But then we want to convert to INT, not FLOAT. is_numeric() doesn't distinct between INT and
FLOAT, so a is_numeric('1E+17') is true, but the Integer of it returns 1, which isn't
perhaps, what the user expects.