PHP evaluates the if
expression in order to know if the epression yields a truthy value or not. There is no other way to find out than to execute the functions that appear in the expression.
In this example it is very clear:
if (sqrt(4) > 1)
PHP must of course call the function sqrt
, except that in this case it is a function without side-effects, while your function test
also performs an echo
. Secondly, it returns a value, while test
doesn't. But that PHP can only find out by executing the function.
Note that the expression can in general return anything, but PHP has rules to what it considers truthy or falsy:
When converting to boolean, the following values are considered FALSE
:
- the boolean
FALSE
itself
- the integer 0 (zero)
- the float 0.0 (zero)
- the empty string, and the string "0"
- an array with zero elements
- an object with zero member variables (PHP 4 only)
- the special type
NULL
(including unset variables)
- SimpleXML objects created from empty tags
Every other value is considered TRUE
(including any resource).
In case a function runs without executing a return
statement, the returned value is null
, so the if
condition will be falsy according to the above rules.