note 72327 deleted from function.is-numeric by cmb
| From: | cmb@php.net | Date: | Sun, 23 Jun 2019 10:33:22 +0000 |
| Subject: | note 72327 deleted from function.is-numeric by cmb | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-211348@lists.php.net to get a copy of this message | ||
Note Submitter: meagar at gmail dot com
----
Miero: Your function doesn't match some special cases: '+1', '-0',
'+0', all of which are valid integers. The easiest and most reliable way to get a
definite integer match is with a regular expression:
function is_intval($value) {
return 1 === preg_match('/^[+-]?[0-9]+$/', $value);
}
This has two "problems" based on your input: it matches both '00' and
'999999999999999999999999999999999' as valid integers.
I'm not sure why you wouldn't want to match "00". Regardless of whether
somebody entered it in a form by accident or on purpose, it /is/ a valid integer, and in most
instances you should accept it.
The second value, "999..." is also a valid integer, even if PHPs internal int type
isn't precise enough to represent it.