Check integer or float values

I have it

$number = 0.5 if (is_float($number)) { echo 'float'; } else { echo 'not float'; } 

and he does not swim. What could be the reason, thanks

+7
php
source share
3 answers

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

See is_numeric . The is_* family checks the actual type of a variable. If you only know if the variable is a number, whether it is int , a float or string , use is_numeric .

If you need to have a nonzero decimal part, you can do:

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

If you only check .(dot) , you don't need typecast , but manipulate it like a string using [strpos][1]() .

 if (strpos($number,'.') !== false) { echo 'true'; }else { echo 'false'; } 
+1
source share

If I understand correctly, using typecast will allow you to check int and float correctly, regardless of language. However, I could be wrong ... I'm not sure if PHP supports decimal locales like comma (,) for specific countries.

0
source share

All Articles