Viewed   71 times

I want to convert following if else condition to nested ternary operator.

if ($projectURL) {
    echo $projectURL;
} elseif ($project['project_url']) {
    echo $project['project_url'];
} else {
    echo $project['project_id'];
}

I have written like following.

echo ($projectURL)?$projectURL:($project['project_url'])?$project['project_url']: $project['project_id'];

But it is found as not working properly.Is this not a right way?

 Answers

5

Ternary operators are tricky thing in PHP, as they are left-associative (unlike all other languages, where it's right-associative). You will need to use parenthesis to tell PHP what you want exactly in this case:

echo ($projectURL ? $projectURL : ($project['project_url'] ? $project['project_url'] : $project['project_id']));
Sunday, October 30, 2022
2

Those parenthesis are what I think is getting you.

Try

$foo = 1;
$bar = ($foo == 1) ? "1" : (($foo == 2)  ? "2" : "other");
echo $bar;
Saturday, August 13, 2022
2

The

(condition) ? /* value to return if condition is true */ 
            : /* value to return if condition is false */ ;

syntax is not a "shorthand if" operator (the ? is called the conditional operator) because you cannot execute code in the same manner as if you did:

if (condition) {
    /* condition is true, do something like echo */
}
else {
    /* condition is false, do something else */
}

In your example, you are executing the echo statement when the $address is not empty. You can't do this the same way with the conditional operator. What you can do however, is echo the result of the conditional operator:

echo empty($address['street2']) ? "Street2 is empty!" : $address['street2'];

and this will display "Street is empty!" if it is empty, otherwise it will display the street2 address.

Sunday, November 13, 2022
 
5

If the results you are returning from the ternary operator are only "true" and "false", then you don't even need the operator. You can just have:

$res = (($rule1 === true) && ($rule2 === false) && ($rule3 === true))

But, to answer your question, yes multiple conditions work perfectly well.

Thursday, August 18, 2022
 
1

I doubt there is a performance difference. They compile to equivalent sequences of bytecodes:

>>> def f():
...   return a if b else c
...
>>> dis.dis(f)
  2           0 LOAD_GLOBAL              0 (b)
              2 POP_JUMP_IF_FALSE        8
              4 LOAD_GLOBAL              1 (a)
              6 RETURN_VALUE
        >>    8 LOAD_GLOBAL              2 (c)
             10 RETURN_VALUE
>>> def g():
...   if b:
...     return a
...   else:
...     return c
...
>>> dis.dis(g)
  2           0 LOAD_GLOBAL              0 (b)
              2 POP_JUMP_IF_FALSE        8

  3           4 LOAD_GLOBAL              1 (a)
              6 RETURN_VALUE

  5     >>    8 LOAD_GLOBAL              2 (c)
             10 RETURN_VALUE
             12 LOAD_CONST               0 (None)
             14 RETURN_VALUE

As with most performance questions, the answer is to measure.

Wednesday, November 16, 2022
 
Only authorized users can answer the search term. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :