note 77119 added to function.ctype-digit
| From: | JustinB at harvest dot org dot REMOVE | Date: | Thu, 16 Aug 2007 04:46:05 +0000 |
| Subject: | note 77119 added to function.ctype-digit | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-129513@lists.php.net to get a copy of this message | ||
@robert at mediamonks dot com & withheld at withheld dot com:
I personally believe more in solutions than problems. If you're concerned about the
possibility of an int type variable being passed to ctype_digit()--and don't want to add
another conditional statement using is_int() to avoid the problem--there's another simple
solution: typecast it.
<?php
// The Problem
$test_values = array(123,'456','7eight9');
foreach($test_values as $test) {
if(ctype_digit($test)) echo 'True' . "\n";
else echo 'False' . "\n";
}
// OUTPUT: False, True, False
// An Easy Solution
$test_values = array(123,'456','7eight9');
foreach($test_values as $test) {
if(ctype_digit((string)$test)) echo 'True' . "\n";
else echo 'False' . "\n";
}
// OUTPUT: True, True, False
?>
----
Server IP: 69.147.83.197
Probable Submitter: 64.118.163.18
----
Manual Page -- http://www.php.net/manual/en/function.ctype-digit.php
Edit -- https://master.php.net/note/edit/77119
Del: integrated -- https://master.php.net/note/delete/77119/integrated
Del: useless -- https://master.php.net/note/delete/77119/useless
Del: bad code -- https://master.php.net/note/delete/77119/bad+code
Del: spam -- https://master.php.net/note/delete/77119/spam
Del: non-english -- https://master.php.net/note/delete/77119/non-english
Del: in docs -- https://master.php.net/note/delete/77119/in+docs
Del: other reasons-- https://master.php.net/note/delete/77119
Reject -- https://master.php.net/note/reject/77119
Search -- https://master.php.net/manage/user-notes.php