note 119312 added to function.ctype-digit
| From: | zorrosNOSPAMwordsman at NOSPAM dot gmail dot com | Date: | Wed, 11 May 2016 00:34:56 +0000 |
| Subject: | note 119312 added to function.ctype-digit | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-205459@lists.php.net to get a copy of this message | ||
If you want to verify whether or not a variable contains only digits, you can type cast it to a
string and back to int and see if the result is identical. Like so:
<?php
// (bool) TRUE if only digits, FALSE otherwise
$isOnlyDigits = (string) (int) $input === (string) $input;
?>
I haven't benchmarked it, but I'm guessing it's significantly faster then regular
expressions.
----
Server IP: 72.52.91.14
Probable Submitter: 24.151.144.232
----
Manual Page -- http://php.net/manual/en/function.ctype-digit.php
Edit -- https://master.php.net/note/edit/119312
Del: integrated -- https://master.php.net/note/delete/119312/integrated
Del: useless -- https://master.php.net/note/delete/119312/useless
Del: bad code -- https://master.php.net/note/delete/119312/bad+code
Del: spam -- https://master.php.net/note/delete/119312/spam
Del: non-english -- https://master.php.net/note/delete/119312/non-english
Del: in docs -- https://master.php.net/note/delete/119312/in+docs
Del: other reasons-- https://master.php.net/note/delete/119312
Reject -- https://master.php.net/note/reject/119312
Search -- https://master.php.net/manage/user-notes.php