Stacking Multiple Ternary Operators in PHP

How to concatenate multiple ternary operator in PHP?

Those parenthesis are what I think is getting you.

Try

$foo = 1;
$bar = ($foo == 1) ? "1" : (($foo == 2) ? "2" : "other");
echo $bar;

Stacking Multiple Ternary Operators in PHP

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

Ternary operator with multiple conditions

This statement will always be true:

($var !== 1 || $var !== 2)

Because $var can never simultaneously be both values, it will always not be at least one of the two values. Which satisfies the || operator.

If you want to know whether $var is one of the two values:

($var === 1 || $var === 2)

If you want to know if $var is neither of the two values, you can negate the condition:

(!($var === 1 || $var === 2))

Or individually negate the operators in the condition and use && instead of || (since all conditions need to be met to prove the negative, instead of just one condition to prove the positive):

($var !== 1 && $var !== 2)

Depending on readability and personal preference.

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") );
?>

multiple ternary operators in wordpress/php

You should wrap it in parentheses:

$special_reports = new \WP_Query([
'post_type' => 'cpac-special-report',
'orderby' => 'menu_order',
'order' => 'ASC',
'posts_per_page' => $data->{"toggle_multi_status"} == 1 ? 3 :
($data->{"toggle_multi_status"} == 2 ? 1 :
($data->{"toggle_multi_status"} == 3 ? 2 :
($data->{"toggle_multi_status"} == 4 ? 1:4)))
]);

PHP multiple ternary operator not working as expected

Because what you've written is the same as:

echo (true ? 1 : true) ? 2 : 3;

and as you know 1 is evaluated to true.

What you expect is:

echo (true) ? 1 : (true ? 2 : 3);

So always use braces to avoid such confusions.

As was already written, ternary expressions are left associative in PHP. This means that at first will be executed the first one from the left, then the second and so on.

php ternary operator - doesn't short circuit with multiple if's

You forgot the parenthesis

function graderator($grade) {
return (
$grade < 65 ? 'F' :
($grade < 70 ? 'D' :
($grade < 80 ? 'C' :
($grade < 90 ? 'B' : 'A')))
);
};

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.



Related Topics



Leave a reply



Submit