Please answer this simple SPAM challenge: max(six, seven)?
(Example: nine)

The Note You're Voting On

Martin K
13 years ago
At 04-Feb-2005 05:13, tom at darlingpet dot com said:
> It's also a good idea to use parenthesis when using something SIMILAR to:
> <?php
> echo (trim($var)=="") ? "empty" : "not empty";

No, it's a BAD idea.

All the short-circuiting operators, including the ternary conditional operator, have LOWER precedence than the comparison operators, so they almost NEVER need parentheses around their subexpressions.

Inserting the parentheses suggested above does not change the meaning of the code, but their use misleads inexperienced programmers to expect that things like this will work in a similar manner:

function my_print($a) { print($a); }
my_print (trim($var)=="") ? "empty" : "not empty";

when of course it doesn't.

Rather than worrying that code doesn't work as expected, simply learn the precedence rules ( so that one expects the right things.

<< Back to user notes page

To Top