9 Answers
9

People will tell you that ?: is "the ternary operator". This is wrong. ?: is a ternary operator, which means that it has three operands. People wind up thinking its name is "the ternary operator" because it's often the only ternary operator a given language has.

I don't disagree, but I sometimes feel that people take their conditional operator hate too far. I would say that ?: should be used whenever it makes the code cleaner and clearer -- and that we can assume, for purposes of "clearer", that the person reading the code does readily understand what ?: does.
–
chaosJul 3 '09 at 17:43

Of course, in good languages, "if, then, else" is an expression. ;)
–
Deniz DoganJul 3 '09 at 18:15

4

Personally i love condition expressions and find them very easy to read. Once you understand that they're an 'IF' shortcut, you wonder what was so hard about them. But each to his own i guess. :o)
–
Gary WilloughbyJul 18 '09 at 17:05

Going off-topic slightly, when you're in a 'view/template' (if you're seperating your concerns through the MVC paradigm), you want as little server-side logic in there as possible. So, using ternaries and other short-hand code is sometimes the best way forward. By "other short-hand code", I mean:

if ($isWinner) :
// Show something cool
endif;

Note, I personally do not like this kind of shorthand if / endif nonsense

How fast is the ternary operator?

People LIKE micro-optimisations. They just do. So for some, it's important to know how much faster things like ternaries are when compared with normal if / else statements.

Oh wait, no it's not. It's only a lot if you're doing thousands upon thousands of them in a row, repeatedly. Which you won't be. So don't worry about speed optimisation at all, it's absolutely pointless here.

When not to use ternaries

Your code should be:

Easy to read

Easy to understand

Easy to modify

Obviously this is subject to the persons intelligence and coding knowledge / general level of understanding on such concepts when coming to look at your code. A single simple ternary like the previous examples are okay, something like the following, however, is not what you should be doing:

Ternaries really are simple and nothing to get too worked up about. Don't consider any speed improvements, it really won't make a difference. Use them when they are simple and look nice, and always make sure your code will be readable by others in the future. If that means no ternaries, then don't use ternaries.

This is sometimes known as the ternary conditional operator. Ternary means that it has three arguments, as x ? y : z. Basically, it checks if x is true; if it is, then put y instead of this operation, otherwise z.

Conditional operator ?: is an operator which is used to check a condition and select a value depending on the value of the condition. It is expressed in the following form:

variable = condition ? expression1 : expression2;

It works as follows...

Firstly, condition is evaluated.

If the condition is true, then expression1 is evalauated. And the value of expression1 is assigned to the variable.

If the condition is false, then expression2 is evaluated. And the value of expression2 is assigned to the variable.

For example:

x = (a>b) ? 5 : 9

In this, for x, firstly the condition (a>b) is evaluated. If this condition becomes true, then x will become the value 5 (ie, x=5). But if the condition (a>b) becomes false, then x will attain the value 9 (ie, x=9).

Ternary Operator

Sometimes conditional operator ?: is also called a ternary operator. This is so because it involves three operands. For example:

x ? y : z

Here, x,y and z are the three operands. If condition x is true, then value y is assigned otherwise value z is assigned.

Right, but the entire if-else block would have to evaluate to something in order for it to be equivalent to the operator. But since the OP discards the value... I guess this code is okay?
–
poundifdefJul 3 '09 at 17:40

Not sure what you mean. The only equivalence that matters here is functional equivalence, no?
–
MatWJul 4 '09 at 8:10