Understanding Nested PHP Ternary Operator

Understanding nested PHP ternary operator

You need to bracket the ternary conditionals:

<?php

for ($a=0; $a < 7; $a++) {
echo (
$a == 1 ? 'one' :
($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 == 1 ? 'one' : $a == 2)
? 'two' : $a == 3) ? 'three' :
$a == 5) ? 'four' : 'other');

So for $a=2, you get:

  echo (
((($a==2) ? 'two' : $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.

Using nested ternary operators

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;

PHP nested ternary if condition

use below code

<?php
$var = 4;
echo $current = (($var > 2) ? "gr than 2" : (($var > 6) ? "gr than 6" : "not gr than 2 or 6") );
?>

PHP - Nested Ternary Operators

Instead of using ternary operators for this, use a switch statement. For example:

switch ($finalGrade) {
case ($finalGrade >= 93 && $finalGrade < 97):
$letterGrade = "A";
break;
//continue $finalGrade checks
}

You can also use an elseif statement.

if ($finalGrade >= 93 && $finalGrade < 97) { 
$lettergrade = "A";
}
elseif($finalGrade >= 90 && $finalGrade < 93) {
$letterGrade = "A-";
}
elseif($finalGrade >= 87 && $finalGrade < 90) {
$letterGrade = "B+";
}
//...continue checks....
else {
$letterGrade = "F";
}

How does this nested ternary expression work?

I had hear of php having "left associativity" with ternary, though I never understood what it meant:

Take

$bool ? "a" : $bool ? "b" : "c"
Right associativity is: $bool ? "a" : ($bool ? "b" : "c")
Left associativity is : ($bool ? "a" : $bool) ? "b" : "c"
So in the end php will always it evaluate to either b or c.

Bonus:

$bool ? $bool ? "c" : "b" : "a"
Here is a syntax that I think wouldn't change meaning based on associativity.
I wonder whether people managed to find a pretty indentation for this variant.

PHP Ternary Operators don't work as expected?

The ternary operator is left associative unlike most other languages such as C#. The code:

$active = is_null($param)
? true
: is_string($param)
? (strtolower($param) === 'true')
: true;

is evaluated as follows:

$active = ((is_null($param) ? true : is_string($param))
? (strtolower($param) === 'true') : true);

You must explicitly add parenthesis to make sure ?: works the way it does in familiar languages:

$active = is_null($param)
? true
: (is_string($param)
? (strtolower($param) === 'true')
: true);

corresponding nested ternary operator in php?

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']));

Error with nested Ternary Operators Requires Explicit Parentheses in PHP 8.0

This can be turned into a single if statement.

if (!empty($aboutus_feature1_title) || !empty($aboutus_feature1_text) ||
!empty($aboutus_feature2_title) || !empty($aboutus_feature2_text) ||
!empty($aboutus_feature3_title) || !empty($aboutus_feature3_text) ||
!empty($aboutus_feature4_title) || !empty($aboutus_feature4_text)) {
$there_is_skills = 'yes';
} else {
$there_is_skills = '';
}


Related Topics



Leave a reply



Submit