matheo's example is the classic act of using a hammer to set a screw. (No offense...)
As programmers, we <b>must</b> become comfortable that the result of a logic-expression <i>is a value</i> just like any other.
Therefore instead of:
<?php
$a = 1 ;
$b = 2 ;
$is_a_bigger = ($a > $b) ? 1 : 0 ;
echo $a_is_bigger ;
# returns 0
?>
Use this:
<?php
$a = 1;
$b = 2;
$a_is_bigger = $a > $b;
...
?>
You read it as "variable $a_is_bigger is set to the result of the comparison of $a to $b". Compare that to the read-thru of the upper example: "variable $is_a_bigger is set to true if the is-greater comparison of $a to $b is true, and set to false if the comparison is false." How redundant. The only thing more redundant is the ever increasingly popular 'duh' logic of today's <i>modern</i> PHP programming:
<?php
$a = 1;
$b = 2;
if( $a > $b )
{
$a_is_bigger = true ;
}
else if( $b >= $a )
{
$a_is_bigger = false ;
}
?>
8 lines to do what one simple construction can do ... which if we <i>learn to recognize it</i> vastly speeds the reading and appreciation of the purpose of the arbitrary algorithm.
The <b>only constructive criticism</b> that I've heard about the use of both ternary operators and logic-evaluation-direct-assign is that neither of them are easy to debug by adding 'prints' or 'echos' to their interiors. Point well made. Yet, if they are as simple as the above examples, it must be argued that the use of 8 lines to accomplish what a single comparison-assignment could do may well have resulted in more potential errors. (For instance, did I remember to put the '=' in the second comparison as the exact compliment of the first?)
rlynch AT lynchmarks DOT com