note 87113 added to function.ctype-digit
| From: | Anonymous at osu1 dot php dot net | Date: | Wed, 19 Nov 2008 21:56:29 +0000 |
| Subject: | note 87113 added to function.ctype-digit | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-146729@lists.php.net to get a copy of this message | ||
Indeed, ctype_digit only functions correctly on strings. Cast your vars to string before you test
them. Also, be wary and only use ctype_digit if you're sure your var contains either a string
or int, as boolean true for ex will convert to int 1.
To be truly safe, you need to check the type of the var first. Here's a wrapper function that
improves upon ctype_digit's broken implementation:
<?
// replacement for ctype_digit, to properly
// handle (via return value false) nulls,
// booleans, objects, resources, etc.
function ctype_digit2 ($str) {
return (is_string($str) || is_int($str) || is_float($str)) &&
ctype_digit((string)$str);
}
?>
If, like me, you're not willing to take a chance on ctype_digit having other problems, use this
version:
<?
// replacement for ctype_digit, to properly
// handle (via return value false) nulls,
// booleans, objects, resources, etc.
function ctype_digit2 ($str) {
return (is_string($str) || is_int($str) || is_float($str)) &&
preg_match('/^\d+\z/', $str);
}
?>
----
Server IP: 66.207.199.35
Probable Submitter: 69.70.125.250
----
Manual Page -- http://www.php.net/manual/en/function.ctype-digit.php
Edit -- https://master.php.net/note/edit/87113
Del: integrated -- https://master.php.net/note/delete/87113/integrated
Del: useless -- https://master.php.net/note/delete/87113/useless
Del: bad code -- https://master.php.net/note/delete/87113/bad+code
Del: spam -- https://master.php.net/note/delete/87113/spam
Del: non-english -- https://master.php.net/note/delete/87113/non-english
Del: in docs -- https://master.php.net/note/delete/87113/in+docs
Del: other reasons-- https://master.php.net/note/delete/87113
Reject -- https://master.php.net/note/reject/87113
Search -- https://master.php.net/manage/user-notes.php