Ternary operator for 3 conditions in php

I use ternary operators alot but I can't seem to stack multiple ternary operator inside each other.

I am aware that stacking multiple ternary operator would make the code less readable but in some case I would like to do it.

This is what I've tried so far :

$foo = 1;
$bar = [ $foo == 1 ] ? "1" : [ $foo == 2 ] ? "2" : "other";
echo $bar; // display 2 instead of 1

What is the correct syntax ?

asked Jun 1, 2011 at 14:43

0

Those parenthesis are what I think is getting you.

Try

$foo = 1;
$bar = [$foo == 1] ? "1" : [[$foo == 2]  ? "2" : "other"];
echo $bar;

answered Jun 1, 2011 at 14:45

0

The problem is that PHP, unlike all other languages, makes the conditional operator left associative. This breaks your code – which would be fine in other languages.

You need to use parentheses:

$bar = $foo == 1 ? "1" : [$foo == 2 ? "2" : "other"];

[Notice that I’ve removed the other parentheses from your code; but these were correct, just redundant.]

answered Jun 1, 2011 at 14:46

Konrad RudolphKonrad Rudolph

512k125 gold badges915 silver badges1191 bronze badges

1

You need some parentheses around the right hand operand:

$foo = 1;
$bar = [ $foo == 1 ] ? "1" : [[ $foo == 2 ] ? "2" : "other"];
echo $bar;

PHP's interpreter is broken, and treats your line:

$bar = [ $foo == 1 ] ? "1" : [ $foo == 2 ] ? "2" : "other";

as

$bar = [[ $foo == 1] ? "1" : [ $foo == 2]] ? "2" : "other";

and since that left hand expression evaluates as "true" the first operand of the remaining ternary operator ["2"] is returned instead.

answered Jun 1, 2011 at 14:46

AlnitakAlnitak

328k70 gold badges402 silver badges485 bronze badges

4

You could write this correctly thus:

$bar = [$foo == 1] ? "1" : [[$foo == 2] ? "2" : "other"];

[i.e.: Simply embed the 'inner' ternary operator in parenthesis.]

However, I'd be really tempted not to do this, as it's about as readable as a particularly illegible thing that's been badly smudged - there's never any excuse for obfuscating code, and this borders on it.

answered Jun 1, 2011 at 14:46

John ParkerJohn Parker

53.6k11 gold badges128 silver badges128 bronze badges

Put parenthesis around each inner ternary operator, this way operator priority is assured:

$bar = [ $foo == 1 ] ? "1" : [[ $foo == 2 ] ? "2" : "other"];

answered Jun 1, 2011 at 14:45

fakenfaken

6,3224 gold badges24 silver badges27 bronze badges

Just stack up the parenthesis, and you've got it:

$bar = [$foo==1? "1" : [$foo==2? "2" : "other"]];

As an aside, if you've got many clauses, you should consider using a switch:

switch [ $bar ] {
  case 1:  echo "1";
  case 2:  echo "2";
  default: echo "other";
}

If the switch gets long, you can wrap it in a function.

answered Jun 1, 2011 at 14:46

cslcsl

10.7k5 gold badges53 silver badges88 bronze badges

$foo = 1;
$bar = [ $foo == 1 ] ? "1" : [[ $foo == 2 ] ? "2" : "other"];
echo $bar;

Just use extra [ ] and it will work

answered Jun 1, 2011 at 14:48

SergSerg

5381 gold badge3 silver badges6 bronze badges

Add the parenthesis:

$bar = [ $foo == 1 ] ? "1" : [[ $foo == 2 ] ? "2" : "other"];

answered Jun 1, 2011 at 14:45

Yuri StukenYuri Stuken

12.2k1 gold badge25 silver badges23 bronze badges

Can ternary operator have three conditions?

The conditional [ternary] operator is the only JavaScript operator that takes three operands: a condition followed by a question mark [ ? ], then an expression to execute if the condition is truthy followed by a colon [ : ], and finally the expression to execute if the condition is falsy.

How use ternary operator in if condition in PHP?

The syntax for the ternary operator is as follows. Syntax: [Condition] ? [Statement1] : [Statement2]; Condition: It is the expression to be evaluated and returns a boolean value.

Is there a ternary operator in PHP?

The term "ternary operator" refers to an operator that operates on three operands. An operand is a concept that refers to the parts of an expression that it needs. The ternary operator in PHP is the only one that needs three operands: a condition, a true result, and a false result.

Can ternary operator have multiple conditions?

We can nest ternary operators to test multiple conditions.

Chủ Đề