All such conditional statements, including while
and if
, are evaluating the given expression against true
. If the expression results in true
, the statement executes the action. If it results in false
, it won't.
$var == true
is an expression which compares $var
to true
. The result of this expression is either true
or false
. The important point to understand here is expressions. Expressions are things which return values. Try var_dump($var == true)
or var_dump(4 > 6)
. It shows you that the expressions return a boolean value. Here:
if ($var == true)
first $var
is compared to true
, which yields either the value true
or false
, which is then evaluated by if
whether it's true
or false
, which then prompts if
to execute the following statement or not.
In other words: it's redundant.
if ($var)
This simply causes if
to evaluate whether $var
is true
or false
and then execute the following statement. The == true
is essentially already "built in".
The following statements are all essentially equivalent:
if ($var)
if ($var == true)
if (($var == true) == true)
if ((($var == true)) == true) == true)
...