Expressions
We usually call +
, -
*
etc. mathematical operators, <
, >
, ==
of conditional operators, or by comparison, and and
, or
, or &&
, ||
and similar of boolean operators 1.
A set of values, permeated by operators, is what we call "expression", and an expression returns a value.
A "Conditional Expression"
The condition is nothing special, because as said, the >
is only one operator. And the if
simply uses its result to define what it will do.
When you say:
if (x > y) { ...
You are first calculating the value of x > y
. Let’s suppose that by chance x
really be bigger than y
, the result of this "calculation" shall be the value true
, so it would have the same effect as if it were written if (true)
in that particular case.
Likewise, if (x > y)
is equivalent to:
var meu_teste = x > y; // teste recebe o valor true se x > y
if ( meu_teste ) { ...
Only after "calculated" the value of x > y
that the if
will use the returned result. Actually if
"doesn’t even know, doesn’t care" what happened inside the parentheses.
The "Boolian Expression" 1
Likewise, or
and and
could also be stored in var" ;)
var condicoes = true or false; // true || false, etc.
if ( condicoes ) { ...
In short: you’re just saving a value from an operation like any other. Just like a + b
, the operations a > b
, a || b
and a++
return a value at the end.
1. Spelled Boolean in many places, but confirmed in the VOLP that in our language it is with "i"
This is because of the conversion of Boolean to string that php does in
echo
.– bfavaretto
The ideal would be to convert PHP to boolean
false
for string"0"
, No? If the value wasnull
Oh yeah he shouldn’t print anything.– Kazzkiq
True, it would be a bug?
– Marcelo Aymone