How can I determine if (float) 0 == 0 or null in PHP

If the value of the variable 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 the solution found discovers that if $ test is a number using the is_number () function, but can I determine if the float variable is equal to zero?

+7
decimal php zero detect
source share
3 answers

Using === also checks the data type:

 $test = round(0, 2); // float(0.00) if($test === null) // false if($test === 0) // false if($test === 0.0) // true if($test === false) // false 
+13
source share

Use 3 identical signs, not two, to check the type:

 if($test === 0) 
+2
source share

If you use === instead of ==, it will compare and also get errors in data types ... Can you post your answer when using ===? Please check the difference between the two here.

When comparing values ​​in PHP for equality, you can use either the == operator or the === operator. What is the difference between 2? Well, that is pretty simple. The == operator only checks if the left and right values ​​are equal. But the === operator (note that the optional "=") checks whether the left and right values ​​are really equal, and also checks whether they have the same type of variable (for example, whether they are both logical, ints and t .d.).

0
source share

All Articles