I just spent the last 2 hours trying to figure out why my input validation class wasn’t working. The scenario was pretty simple; ensure that a particular field was a three digit number. I have been using is_int() for the past two hours and wondering why I haven’t been getting the results I was expecting. Lo and behold, upon consulting the PHP manual in a nice big highlighted box it clearly states that when validating form input one should use the is_numeric() function instead as all form data are retrieved as strings. I am tired, hungry, miserable and feel like an idiot. Still love PHP though. Goodbye two hours…
Use is_numeric() When Testing Form Data!!!
Comments Map
Location data courtesy of GeoSmartPrevious post: Twitter: Sheepfold For The Masses
Next post: Stupid PayPal!



J from Montana, United States on
Stephen Orr from Walsall, United Kingdom
Corve from Saint Andrew, Jamaica
{ 4 comments… read them below or add one }
You should really use a debugger…
Twitter: @SamuelFolkes
Good point. Had I done so it would have saved me a LOT of time.
Remember that is_numeric() also accepts strings such as “10e2″ als valid numeric values. Try ctype_digit() to check if a string contains only digits. ctype_digit() also returns true on empty strings (since they do not contain anything else than digits) so you will have to check for that as well (test using $string === ”).
I like using type casting too.
IE:
$number = (int) $_GET['number'];
If it’s not a pure digit number submitted, it makes $number blank.