How to check if a variable is numeric?

The PHP PDO method lastInsertId () actually returns a numeric value, but it can also return something completely different, like some weird SQLSTATE code or something else. In any case, it returns a non-numeric value, I would like to register an error. Is there a way to safely verify this?

+1
source share
3 answers

Use

is_int($value) 

or

 is_numeric($value) 
+8
source

is_numeric returns true for numeric strings and false otherwise. Similar function ctype_digit .

+6
source

is_int or is_numeric is a good way.

You can also try to cast the result to (int) and check if it is > 0 .

0
source

Source: https://habr.com/ru/post/1343722/


All Articles