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 OverflowWrap 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;
As of PHP 7 we can use Null coalescing operator
$selectedTemplate = $_POST['selectedTemplate'] ?? $_GET['selectedTemplate'] ?? 0;
Videos
What does the PHP ternary operator do?
Can I nest ternary operators in PHP?
When should I avoid ternary operators?
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.
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'
Others have already suggested the right way of doing it but if you really want to use ternary operator you need to use parenthesis as:
$province = 7;
$Myprovince = (
($province == 6) ? "city-1" :
(($province == 7) ? "city-2" :
(($province == 8) ? "city-3" :
(($province == 30) ? "city-4" : "out of borders")))
);
Updated Link
The ternary operator is evaluated from left to right. So if you don't group the expressions properly, you will get an unexpected result.
PHP's advice is [docs]:
It is recommended that you avoid "stacking" ternary expressions. PHP's behaviour when using more than one ternary operator within a single statement is non-obvious.
Your code actually is evaluated as:
(
(
(
$province == 6 ? "city-1" : $province == 7
) ? "city-2" :
$province == 8
) ? "city-3" : $province == 30
) ? "city-4" : "out of borders";
where it should be
$province == 6 ? "city-1" : (
$province == 7 ? "city-2" : (
$province == 8 ? "city-3" : (
$province == 30 ? "city-4" : "out of borders"
)
)
);
This code might look fine but someone will read it and they will need more time than they should to understand what this code is doing.
You would be better off with something like this:
$map = array( 6 = >'city-1',
7 => 'city-2',
8 => 'city-3',
30 => 'city-4');
$Myprovince = "out of borders";
if(array_key_exists($province, $map)) {
$Myprovince =
province];
}
Or as @Jonah mentioned in his comment:
$Myprovince = isset(
province]) ?
province] : 'out of borders';
The code will be executed front to back. So first
<?php
($var > 2) ? "gr than 2" : ($var > 6)
?>
will result in "gr than 2".
Then the next questionmark will result in gr than 6, because "gr than 2" is equal to true.
Also because of the above it would be good to notice that > 6 and > 2 are both greater than 2, so the whole line is actually quite pointless the way it is written.
The solution would be something like:
<?php
$var = 4;
echo $current = ($var < 2 ? "not gr than 2 or 6" : ($var > 6 ? "gr than 6" : "gr than 2"));
?>
* Edit: *
Thank you for the upvotes. When looking again at this I got lost in my own post, because the logic is so complex. So for others reading this:
The logic the OP posted can be simplified to the following:
<?php
echo true ? "first" : false ? "second" : "third";
The OP would expect this to result in first.
However, it does result in second because first the first part is being executed, and because that part is true the outcome is "second".
use below code
<?php
$var = 4;
echo $current = (($var > 2) ? "gr than 2" : (($var > 6) ? "gr than 6" : "not gr than 2 or 6") );
?>
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>
Although it doesn't directly answer the question, you could avoid the nested-ternary entirely:
<?php
$minlen = array(
"admin" => 16,
"edit" => 12,
"wadmin" => 8,
"wuser" => 8,
);
// Example usage
$match = "admin";
echo $minlen[$match];
?>
This will do the trick.
enclose the false condition in brackets / parentheses
echo ($match == 'admin') ? '16' : (($match == 'edit') ? '12' : '8');
or
echo $match == 'admin' ? '16' : ($match == 'edit' ? '12' : '8');
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']));
As of php 7 we can use Null coalescing operator
echo $projectURL ?? $project['project_url'] ?? $project['project_id'];