If variable value is 0 (float) it will pass all these tests:
$test = round(0, 2); //$test=(float)0
if($test == null)
echo "var is null";
if($test == 0)
echo "var is 0";
if($test == false)
echo "var is false";
if($test==false && $test == 0 && $test==null)
echo "var is mixture";
I assumed that it will pass only if($test == 0)
Only solution I found is detect if $test is number using function is_number(), but can I detect if float variable equal zero?
PHP considers null is equal to zero.
The is_float() function checks whether a variable is of type float or not. This function returns true (1) if the variable is of type float, otherwise it returns false.
A floating point number, is a positive or negative whole number with a decimal point. For example, 5.5, 0.25, and -103.342 are all floating point numbers, while 91, and 0 are not. Floating point numbers get their name from the way the decimal point can "float" to any position necessary.
0 is the integer value of zero, and false is the boolean value of, well, false. To make things complicated, in C, for example, null, 0, and false are all represented the exact same way.
Using ===
checks also for the datatype:
$test = round(0, 2); // float(0.00)
if($test === null) // false
if($test === 0) // false
if($test === 0.0) // true
if($test === false) // false
Use 3 equal signs rather than two to test the type as well:
if($test === 0)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With