note 72327 added to function.is-numeric
| From: | meagar at gmail dot com | Date: | Sun, 14 Jan 2007 03:52:03 +0000 |
| Subject: | note 72327 added to function.is-numeric | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-121622@lists.php.net to get a copy of this message | ||
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.
----
Server IP: 69.147.83.197
Probable Submitter: 74.110.130.103
----
Manual Page -- http://www.php.net/manual/en/function.is-numeric.php
Edit -- https://master.php.net/note/edit/72327
Del: integrated -- https://master.php.net/note/delete/72327/integrated
Del: useless -- https://master.php.net/note/delete/72327/useless
Del: bad code -- https://master.php.net/note/delete/72327/bad+code
Del: spam -- https://master.php.net/note/delete/72327/spam
Del: non-english -- https://master.php.net/note/delete/72327/non-english
Del: in docs -- https://master.php.net/note/delete/72327/in+docs
Del: other reasons-- https://master.php.net/note/delete/72327
Reject -- https://master.php.net/note/reject/72327
Search -- https://master.php.net/manage/user-notes.php