27

I am trying to convert the following code into a Ternary Operator, but it is not working and I am unsure why. I think my problem is that I do not know how to express the elseif operation in ternary format. From my understanding and elseif is performed the same way as an if operation by using the format : (condition) ? 'result'.

if ($i == 0) {
    $top = '<div class="active item">';
} elseif ($i % 5 == 0) {
    $top = '<div class="item">';
} else {
    $top = '';
}

$top = ($i == 0) ? '<div class="active item">' : ($i % 5 == 0) ? '<div class="item">' : '';
4
  • 14
    Whydoyouwantomakeyoursourcecodeunreadable? Commented Oct 31, 2012 at 16:09
  • 1
    I don't, but I want to know how to handle ifelse. Commented Oct 31, 2012 at 16:11
  • As @VolkerK said, nesting ternary operators is not a good idea at all. You should avoid it. Commented Oct 31, 2012 at 16:16
  • 2
    Yes, I agree. I just want to know for my own knowledge. At least, now when I see that line of mess, I will be able to decrypt it :) Commented Oct 31, 2012 at 16:21

4 Answers 4

64
$top = ($i == 0) ? '<div class="active item">' : (($i % 5 == 0) ? '<div class="item">' : '');

you need to add parenthesis' around the entire else block

Sign up to request clarification or add additional context in comments.

Comments

18

The Ternary Operator doesn't support a true if... else if... else... operation; however, you can simulate the behavior by using the following technique

var name = (variable === 1) ? 'foo' : ((variable === 2) ? 'bar' : 'baz');

I personally don't care for this as I don't find it more readable or elegant. I typically prefer the switch statement.

switch (variable) {
    case 1 : name = 'foo'; break;
    case 2 : name = 'bar'; break;
    default : name = 'bas'; break;
}

Comments

5

Too late probably to share some views, but nevertheless :)

  1. Use if - else if - else for a limited number of evaluations. Personally I prefer to use if - else if - else when number of comparisons are less than 5.
  2. Use switch-case where number of evaluations are more. Personally I prefer switch-case where cases are more than 5.
  3. Use ternary where a single comparison is under consideration (or a single comparison when looping), or when a if-else compare is needed inside the "case" clause of a switch structure.
  4. Using ternary is faster when comparing while looping over a very large data set.

IMHO Its finally the developer who decides the trade off equation between code readability and performance and that in turn decides what out of, ternary vs. if else-if else vs. switch-case, can be used in any particular situation.

Comments

2
//Use this format before reducing the expression to one liner
$var=4; //Change value to test
echo "Format result: ";

echo($var === 1)    ? 'one'     : //if      NB.=> $varname = || echo || print || var_dump(ternary statement inside); can only be (placed at the start/wrapping) of the statement. 
    (($var === 2)   ? 'two'     : //elseif
    (($var === 3)   ? 'three'   : //elseif
    (($var === 4)   ? 'four'    : //elseif
    'false'                       //else
    )));                          //extra tip: closing brackets = totalnumber of conditions - 1 

// Then  echo($var === 1)?'one':(($var === 2)?'two':(($var === 3)?'three':(($var === 4)?'four':'false'))); 
echo "<br/>";
var_dump("Short result: ", ($var === 1)?'one':(($var === 2)?'two':(($var === 3)?'three':(($var === 4)?'four':'false'))) );

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.