0

Possible Duplicate:
php validate integer

Currently I'm using this method:

  1. Validate the input using isset($val) && is_numeric($val)
  2. Cast it to int and passing it to the function: DoSomething((int)$val);

So my question is this: Is this the fastest and most practical way to do this? If I cast the value to int, is the int range enough for the maximum number of rows in the mysql database?

Community
  • 1
  • 1
Alireza Noori
  • 14,961
  • 30
  • 95
  • 179

3 Answers3

3

PHP offers integrated basic filter & validation functions.

breiti
  • 1,145
  • 9
  • 17
2

it seems the method is quite redundant.

either validate or cast - no need to do both.

And this is apparently not a place that needs to be "fastest".

Your Common Sense
  • 156,878
  • 40
  • 214
  • 345
1

a quick look at php manual page shoved that:

$foo = 5a;
settype($foo, "integer"); // $foo is now 5   (integer)

And: /* checks if a string is an integer with possible whitespace before and/or after, and also isolates the integer */

$isInt=preg_match('/^\s*([0-9]+)\s*$/', $myString, $myInt);