openfrog
openfrog

Reputation: 40735

How to check if a variable is a numeric value?

PHP PDO's lastInsertId() method actually returns an numeric value, but it may also return something completely different like some strange SQLSTATE code or whatever. In any case it returns not a numeric value I would like to log an error. Is there a way to safely check for that?

Upvotes: 1

Views: 501

Answers (3)

hsz
hsz

Reputation: 152216

is_int or is_numeric is a good way.

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

Upvotes: 0

Matijs
Matijs

Reputation: 2553

Use

is_int($value)

or

is_numeric($value)

Upvotes: 8

Gumbo
Gumbo

Reputation: 655239

is_numeric returns true for numerical strings and false otherwise. A similar function is ctype_digit.

Upvotes: 6

Related Questions