check for integer or float values

The is_numeric() function checks whether a variable is a number or a numeric string. This function returns true if the variable is a number or a numeric string, otherwise it returns false/nothing.

$number = 0.5;
echo is_numeric($number);   //true

OR

$number = "0.5";
echo is_numeric($number);   //true

Read more in:

How to check if a variable is a float in PHP?

How to check if a variable is an Integer in PHP?


Probably $number is actually a string: "0.5".

See is_numeric instead. The is_* family checks against the actual type of the variable. If you only what to know if the variable is a number, regardless of whether it's actually an int, a float or a string, use is_numeric.

If you need it to have a non-zero decimal part, you can do:

//if we already know $number is numeric...
if ((int) $number == $number) {
    //is an integer
}

Tags:

Php