How can i detect if (float)0 == 0 or null in PHP

mrfazolka picture mrfazolka · Jul 30, 2014 · Viewed 16.6k times · Source

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?

Answer

Markus Kottländer picture Markus Kottländer · Jul 30, 2014

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