note 77119 deleted from function.ctype-digit by philip

From: Date: Sun, 22 Feb 2009 21:14:47 +0000
Subject: note 77119 deleted from function.ctype-digit by philip
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-150970@lists.php.net to get a copy of this message
Note Submitter: JustinB at harvest dot org dot REMOVE ---- @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 ?>

« previous php.notes (#150970) next »