Dmitry Makovetskiyd Dmitry Makovetskiyd - 7 months ago 10
PHP Question

Checking if a string is a double or not

I am trying to check in php if a string is a double or not.

Here is my code:

if(floatval($num)){
$this->print_half_star();
}


$num is a string..The problem is that even when there is an int it gives true. Is there a way to check if it is a float and not an int!?

Answer
// Try to convert the string to a float
$floatVal = floatval($num);
// If the parsing succeeded and the value is not equivalent to an int
if($floatVal && intval($floatVal) != $floatVal)
{
    // $num is a float
}
Comments