Is_numeric or numeric preg_match?

I read on the forum that you cannot fully trust is_numeric (). It passes through "0xFF", for example, which is a valid hexadecimal ...

So my question is: can you fool is_numeric ? Do I need to use regex to do it right?

+5
source share
5 answers

This is what is_numeric () is considered to be a number string:

Numeric strings consist of an optional character, any number of digits, an optional decimal part, and an optional exponential part. Thus, + 0123.45e6 is a valid numerical value. A hexadecimal notation (0xFF) is also allowed, but only without the sign, decimal, and exponential parts.

If you want to check if a string of decimal digits is 0-9, you can use ctype_digit () .

+12
source

You can also check using ctype_digit()to check if this is really a true number.

+2
source

Regex, , , . , .

0

? "x" is_numeric() .

0

, - , :

function isInteger($value){
    return (is_numeric($value) ? intval($value) == $value : false);
}

If you also want to check for floats, this will not work :)

0
source

All Articles