PHP Quicky - Verification of user input: Digits

2 replies
Hello all,

Time for another PHP quicky.

If you need to verify if a submitted field is a numerical value, PHP offers a handy function called :

is_numeric()

What this function does is return a 'true' if the value is an integer or numeric string, 'false' otherwise. For example:
Code:
$number = 45;  echo is_numeric($number);  // prints 1 -> value is numeric
$number = '3';  echo is_numeric($number);  // prints 1 -> value is numeric
$number = 'abc'; echo is_numeric($number);  // prints 0 -> value is NOT numeric
One thing to remember when using this function is that testing the number (or numerical string) '0' will return true.

I often use this test in script to make sure the value is a valid id (excludes '0' as an ID):
Code:
if (!empty($value ) && is_numeric($value)) {
   ..do something
}
WP4H
#digits #input #php #quicky #user #verification
  • Profile picture of the author fazlerocks
    nice info.. thanks dude!!
    {{ DiscussionBoard.errors[3182056].message }}
  • Profile picture of the author jameswatson2002
    helpful u can use mysql_real_escape_string to filter inputs for bad characters also ! and trim to remove spaces !
    {{ DiscussionBoard.errors[3182321].message }}

Trending Topics