I have this
$number = 0.5
if (is_float($number))
{
echo 'float';
}
else
{
echo 'not float';
}
and it echos not float. what could be the reason thanks
I have this
$number = 0.5
if (is_float($number))
{
echo 'float';
}
else
{
echo 'not float';
}
and it echos not float. what could be the reason thanks
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
}