开发者

How to check if a variable is a numeric value?

开发者 https://www.devze.com 2022-12-15 11:41 出处:网络
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 r

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?


Use

is_int($value)

or

is_numeric($value)


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


is_int or is_numeric is a good way.

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

0

精彩评论

暂无评论...
验证码 换一张
取 消