PHP IF statement for Boolean values: $var === true vs $var

MarioRicalde picture MarioRicalde · Nov 3, 2009 · Viewed 93.7k times · Source

I know this question is not really important.. however I've been wondering:

Which of the following IF statements is the best and fastest to use?

<?php

$variable = true;

if($variable === true)
{
    //Something
}

if($variable)
{
    // Something
}


?>

I know === is to match exactly the boolean value. However is there really any improvement?

Answer

Gumbo picture Gumbo · Nov 3, 2009

Using if ($var === true) or if ($var) is not a question of style but a question of correctness. Because if ($var) is the same as if ($var == true). And == comparison doesn’t check the type. So 1 == true is true but 1 === true is false.