Wrap it in parentheses:

$selectedTemplate = isset($_POST['selectedTemplate'])
                  ? $_POST['selectedTemplate']
                  : (
                       isset($_GET['selectedTemplate'])
                       ? $_GET['selectedTemplate']
                       : 0
                  );

Or even better, use a proper if/else statement (for maintainability):

$selectTemplate = 0;

if (isset($_POST['selectedTemplate'])) {
    $selectTemplate = $_POST['selectedTemplate'];
} elseif (isset($_GET['selectedTemplate'])) {
    $selectTemplate = $_GET['selectedTemplate'];
}

However, as others have pointed out: it would simply be easier for you to use $_REQUEST:

$selectedTemplate = isset($_REQUEST['selectedTemplate'])
                  ? $_REQUEST['selectedTemplate']
                  : 0;
Answer from Joseph Silber on Stack Overflow
🌐
PHP
wiki.php.net › rfc › ternary_associativity
PHP RFC: Deprecate left-associative ternary operator
However, as an exception, explicit parenthesis are not required when combining two short ternaries: 1 ?: 2 ?: 3; // ok (1 ?: 2) ?: 3; // ok 1 ?: (2 ?: 3); // ok · The reason is that ($a ?: $b) ?: $c and $a ?: ($b ?: $c) will always yield the same result, even though the left-associative version will arrive at it in a less efficient manner. Parentheses are also not required when nesting into the middle operand, as this is always unambiguous and not affected by associativity:
People also ask

Can I nest ternary operators in PHP?
Yes. You can write a ternary inside another. Read it carefully to avoid confusion.
🌐
flatcoding.com
flatcoding.com › home › php ternary operator: how to write short expressions
PHP Ternary Operator: How to Write Short Expressions - FlatCoding
When should I avoid ternary operators?
Avoid them in long conditions or complex logic. Use if-else for better readability.
🌐
flatcoding.com
flatcoding.com › home › php ternary operator: how to write short expressions
PHP Ternary Operator: How to Write Short Expressions - FlatCoding
🌐
Lindevs
lindevs.com › nested-ternary-operators-requires-explicit-parentheses-in-php-8-0
Nested Ternary Operators Requires Explicit Parentheses in PHP 8.0 | Lindevs
January 25, 2023 - In PHP 7.4 we will get a deprecation warning and the code prints Negative instead of Positive because ternary operator is left-associative: Deprecated: Unparenthesized `a ? b : c ? d : e` is deprecated. Use either `(a ? b : c) ? d : e` or `a ? b : (c ? d : e)` in main.php on line 4 Negative · Since PHP 8.0, the nested ternary operators require explicit parentheses and using without them, a fatal error occurs.
🌐
Stitcher
stitcher.io › blog › shorthand-comparisons-in-php
Shorthand comparisons in PHP | Stitcher.io
The reason because is that the ternary operator in PHP is left-associative, and thus parsed in a very strange way. The above example would always evaluate the $elseCondition part first, so even when $firstCondition would be true, you'd never see its output. I believe the right thing to do is to avoid nested ...
🌐
FlatCoding
flatcoding.com › home › php ternary operator: how to write short expressions
PHP Ternary Operator: How to Write Short Expressions - FlatCoding
July 1, 2025 - You can write nested ternary operators. Use them to choose from more than two options. Read them carefully. Break them into clear levels. Here’s the breakdown step-by-step. PHP checks the first condition. If true, it returns the first value. If false, it evaluates the next ternary.
Top answer
1 of 4
32

You need to bracket the ternary conditionals:

<?php

for (a < 7; $a++) {
  echo (
    a == 2 ? 'two' :
    ($a == 3 ? 'three' :
    ($a == 5 ? 'four' : 'other'))));
    echo "\n";
    // prints 'four'
}
exit;
?>

returns:

other
one
two
three
other
four
other

as you'd expect.

See the note at the bottom of "Ternary operators" at PHP Ternary operator help.

The expressions are being evaluated left to right. So you are actually getting:

  echo (
    (((a == 2)
     ? 'two' : $a == 3) ? 'three' :
    $a == 5) ? 'four' : 'other');

So for $a=2, you get:

  echo (
    (((a == 3) ? 'three' :
    $a == 5) ? 'four' : 'other');

and then

  echo (
    ((true ? 'two' : $a == 3) ? 'three' :
    $a == 5) ? 'four' : 'other');

and then

  echo (
    ('two' ? 'three' : $a == 5) ? 'four' : 'other');

and then

  echo (
    'three' ? 'four' : 'other');

and so echo 'four'.

Remember that PHP is dynamically typed and treats any non-zero non-null values as TRUE.

2 of 4
6

On the Comparison Operators page in the PHP Manual they explain that PHP's behavior is "non-obvious" when nesting (stacking) ternary operators.

The code you've written is like this:

$a = 2;

echo
  (((a == 2) ? 'two'   :
     $a == 3) ? 'three' :
     $a == 5) ? 'four'  : 
       'other'
  ;

// prints 'four'

As $a is 2 and both 'two' and 'three' are TRUE as well, you get "four" as the result, as you don't compare any longer if 'four' is TRUE or not.

If you want to change that, you have to put the brackets at different places [also noted by: BeingSimpler and MGwynne]:

$a = 2;
echo 
  (a == 2 ? 'two'   :
  ($a == 3 ? 'three' :
  ($a == 5 ? 'four'  : 
     'other'))))
  ;

// prints 'two'
🌐
GitHub
github.com › phpstan › phpstan › issues › 3942
[PHP 8] Nested ternary operator needs to have parentheses around it : false positive ? · Issue #3942 · phpstan/phpstan
May 4, 2020 - [PHP 8] Nested ternary operator needs to have parentheses around it : false positive ?#3942 · Copy link · gnutix · opened · on Oct 12, 2020 · Issue body actions · I have a code containing parenthesis and the issue is still reported. https://phpstan.org/r/99e27548-b5ef-4701-be1b-4586579014f5 ·
Published   Oct 12, 2020
Find elsewhere
🌐
SmartFlow
smartflow.wordpress.com › 2018 › 01 › 11 › php-nested-ternary-operator-order
PHP Nested Ternary Operator Order | SmartFlow
January 11, 2018 - Now let’s guess what is the result of variable text? The expected value should be A, which is already correct in other languages, but PHP​ produces B. Well, this is not a great discovery but many PHP developers may be missing this after all, so I think it’s worth archiving.
🌐
PHP
php.net › manual › en › migration74.deprecated.php
PHP: Deprecated Features - Manual
Nested ternary operations must explicitly use parentheses to dictate the order of the operations.
🌐
Medium
medium.com › geekculture › how-to-execute-multiple-actions-in-a-ternary-block-php-38548d0236c6
How to Execute Multiple Actions in a Ternary Block — PHP | by Simon Ugorji | Geek Culture | Medium
December 24, 2021 - But it doesn’t work and I will ... logic. This is unlike what’s made available in JavaScript. Only use Ternary operators for a single condition in PHP, and you may execute multiple actions within that conditio...
🌐
Reddit
reddit.com › r/phphelp › how to simplify nested ternary operator?
r/PHPhelp on Reddit: How to simplify nested ternary operator?
June 4, 2024 -

As in the title, is there any way to simplify this nested ternary operator?

<select name="department" id="department" required>
    <option value=""></option>

    <?php foreach ($departments as $department) : ?>
        <option value="<?= $department['id'] ?>" 
            <?= isset($_POST['submit']) ? 
                ($department['id'] === $_POST['department_id'] ? 
                    'selected' 
                : 
                    '') 
            : 
                ($department['id'] === $asset['department_id'] ? 
                    'selected' 
                : 
                    '') ?>
        >
            <?= $department['name'] ?>
        </option>
    <?php endforeach ?>
</select>
🌐
GitHub
gist.github.com › fmtarif › 629b97b430375e11e15a
php non obvious behavior of nested ternary
#php non obvious behavior of nested ternary · Raw · nested-ternary.php · This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
🌐
SonarSource
rules.sonarsource.com › php › rspec-3358
PHP static code analysis: Ternary operators should not be ...
Unique rules to find Bugs, Vulnerabilities, Security Hotspots, and Code Smells in your PHP code
🌐
Scientech Easy
scientecheasy.com › home › blog › ternary operator in php
Ternary Operator in PHP - Scientech Easy
October 21, 2024 - Example 4: <?php $grade = 85; $status = ($grade >= 90) ? 'A' : (($grade >= 80) ? 'B' : (($grade >= 70) ? 'C' : 'F')); echo $status; ?> ... A nested ternary operator in PHP allows you to place one ternary operator inside another.
🌐
W3Schools
w3schools.in › php › operators › ternary-operator
PHP Ternary Operator
This conditional statement takes its execution from left to right. Using this ternary operator is not only an efficient solution but the best case with a time-saving approach. It returns a warning while encountering any void value in its conditions. The syntax of using the conditional operator ...
🌐
Codementor
codementor.io › community › ternary operator in php | how to use the php ternary operator
Ternary Operator in PHP | How to use the PHP Ternary Operator | Codementor
July 19, 2019 - The ternary operator is a conditional operator that decreases the length of code while performing comparisons and conditionals. This method is an alternative for using if-else and nested if-else ...