Using short if statement in programming
In many programing languages it is possible to shorten if statements using what’s called the ternary operator. It is sometimes referred as the “one line if statement” or the “short if statement”. This can help at times to produce cleaner code, however use this operator wisely as it is not always best to be used for more complicated statements.
PHP Example of an if statement
1 2 3 4 5 6 7 8 | if($nFoo > 0) { echo "I'm at the work."; } else { echo "I'm at home."; } |
PHP Example using the ternary operator
1 | echo $nFoo > 0 ? "I'm at the work." : "I'm at home."; |
The expression (expr1) ? (expr2) : (expr3) evaluates to expr2 if expr1 evaluates to TRUE, and expr3 if expr1 evaluates to FALSE.
Source: Wikipedia on Ternary Operator
Source: PHP Manual: Comparison Operators


October 8th, 2008 at 4:27 pm
In PHP you can also pull those echo statements out and have the ternary statement just return a string to a single echo, for incrementally cleaner code:
echo $nFoo > 0 ? “I’m at the work.” : “I’m at home.”;
October 8th, 2008 at 4:32 pm
One drawback to using the ternary operator is that you now have a logic branch in a single “line” of code. This makes it hard to measure code coverage from tests, for example. It also makes diffs less clear when you commit code changes to source control.
I never use the ternary operator when each predicate is a full statement of code. I use the ternary operator only when I need each predicate to return a value because the whole expression returns a value.
As soon as you need either predicate to be two statements, you must rewrite the expression into a full if/then/else construct anyway.
It should not be your goal to write code that has fewer lines. It should be your goal to write code that is easier to test, easier to change, easier to document, easier to maintain.
October 8th, 2008 at 4:58 pm
echo ( $nFoo > 0 ? “I’m at the work.” : “I’m at home.” );
I think that’s better syntax.
October 8th, 2008 at 5:47 pm
Your second example won’t work, echo can’t be used inside an expression, youd have to use print (that’s the only difference between the two btw., print always returns true and vcan be used in expressions, echo can’t) or you have to write the echo outside the ternary, which i consider nicer.
echo $condition ? “yes” : “no”;
But don’t abuse ternaries, I saw people widely nesting them - unreadable that is
October 8th, 2008 at 7:21 pm
Especially since PHP does it backwards with regards to that if you have nested ternary operators.
You could also eliminate it down to one echo and just have it evaluate which string to echo out.
October 8th, 2008 at 7:29 pm
I have seen some horrific uses of the ternary operator. I would rather it not be used than abused.
Something I wrote on the topic:
http://wultsch.com/blog/2008/08/17/why-php-sucks/
October 8th, 2008 at 7:35 pm
I used to hate the ternary operator, because it puts two possible values for a variable on one line and I’d like to keep my style for if/else statements consistent.
It is much easier to put these lines of code on one line than taking up six lines of code. Not only is it a matter of space, but for simple variable definitions it makes it easy to have a default value.
Ex:
$page_num = ($_REQUEST[’page’]) ? $_REQUEST[’page’] : 1;// Default 1st page.
Jeremy
October 9th, 2008 at 12:53 am
This example doesn’t work as echo is not an expression and the ternary operator expects his arguments to be expressions. The correct example should be :
echo $nFoo > 0 ? “I’m at the work.” : “I’m at home.” ;
October 9th, 2008 at 3:54 am
$nFoo > 0 ? echo “I’m at the work.” : echo “I’m at home.”;
This is the overcomplicated version, still. You can use this:
echo $nFoo > 0 ? “I’m at the work.” : “I’m at home.”;
Or even:
echo “I’m at “.($nFoo > 0 ? “the work”:”home”);
October 9th, 2008 at 6:14 am
A better example would have been
echo ($nFoo > 0) ? “I’m at the work.” : “I’m at home.”;
as one of the main differences between an if block and a ternary (besides the amount of keystrokes needed) is that the ternary is an expression and so has a result value.
IMHO it is also good style to put ternary parts on different lines unless they are *really* short like “$leap_year ? 29 : 28″:
echo ($nFoo > 0)
? “I’m at the work.”
: “I’m at home.”;
October 10th, 2008 at 6:05 am
You also move “echo” function out of statment:
echo $nFoo > 0 ? “I’m at the work.” : “I’m at home.”;
I personaly add braces when using this type of statment to make it more readable:
echo (($nFoo > 0) ? “I’m at the work.” : “I’m at home.”);
October 14th, 2008 at 1:11 pm
Thanks to everyone for the great feedback! I have made changes to the ternary operator example to remove the unnecessary echo statements.