12

Possible Duplicate:
What's the correct way to test if a variable is a number in PHP?

I have two variables coming in from a form:

$minNo = mysql_real_escape_string($_GET["minNo"]);
$maxNo = mysql_real_escape_string($_GET["maxNo"]);

How can I check whether they are numerical values or not?

Community
  • 1
  • 1
CJS
  • 351
  • 2
  • 5
  • 15

4 Answers4

15

Try is_numeric.

Finds whether the given variable is numeric. Numeric strings consist of optional sign, any number of digits, optional decimal part and optional exponential part. Thus +0123.45e6 is a valid numeric value. Hexadecimal notation (0xFF) is allowed too but only without sign, decimal and exponential part.

j08691
  • 204,283
  • 31
  • 260
  • 272
14

Use is_numeric function, it returns bool value:

is_numeric($element)
MarcinJuraszek
  • 124,003
  • 15
  • 196
  • 263
6

You can use is_numeric() to check if the number is numeric.

To validate as an integer, use...

$validInt = filter_var($minNo, FILTER_VALIDATE_INT);
alex
  • 479,566
  • 201
  • 878
  • 984
  • In my case I wanted to check that $_GET['id'] was an integer, otherwise exit. This seems to be the best method. Of course using prepared statement following this. – drooh Dec 10 '18 at 04:23
3

using is_numeric() function...