Corresponding Nested Ternary Operator in PHP

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

what is the difference between these two lines

Consider this code:

echo (true?"Left is first":(true?"Right is first":""));

Left is first

Versus

echo (true?"Left is first":true?"Right is first":"");

Right is first

The exaplanation can be found at http://php.net/manual/en/language.operators.comparison.php#language.operators.comparison.ternary.

In short, in the second case PHP will evaluate true?"Left is first":true as the condition for the ternary expression. This will evaluate to Left is first which evaluates to true and therefore Right is first will be echoed

Why do long and short forms of the ternary operator evaluate differently?

You already start with a wrong assumption that these 2 code lines are identical, because they are not. The ternary (expression ? IF TRUE : IF FALSE) operator is left associative.

So if you go through step by step you maybe see it better:

First ternary line:

echo 0 ?: 1 ?: 2 ?: 3; //1

With parentheses:

echo ((0 ?: 1) ?: 2) ?: 3; //1
└──────┘ //0 → FALSE
↓ //Second expression: 1
echo ((1) ?: 2) ?: 3; //1
└────────┘ //1 → TRUE
↓ //First expression: 1
echo (1) ?: 3; //1
└──────┘ //1 → TRUE
↓ //First expression: 1
echo 1; //1

Second ternary line:

echo 0 ? 0 : 1 ? 1 : 2 ? 2 : 3; //2

With parentheses:

echo ((0 ? 0 : 1) ? 1 : 2) ? 2 : 3; //2
└─────────┘ //0 → FALSE
↓ //Second expression: 1
echo ((1) ? 1 : 2) ? 2 : 3; //2
└───────────┘ //1 → TRUE
↓ //First expression: 1
echo (1) ? 2 : 3; //2
└─────────┘ //1 → TRUE
↓ //First expression: 2
echo 2; //2

Align subsequent ternary operators in PhpStorm

That is not possible .. and at the moment is not even planned for any specific version (based on "Backlog" status of the corresponding ticket).

https://youtrack.jetbrains.com/issue/WI-17880 -- watch this ticket (star/vote/comment) to get notified on any progress.

To ternary or not to ternary?

Use it for simple expressions only:

int a = (b > 10) ? c : d;

Don't chain or nest ternary operators as it hard to read and confusing:

int a = b > 10 ? c < 20 ? 50 : 80 : e == 2 ? 4 : 8;

Moreover, when using ternary operator, consider formatting the code in a way that improves readability:

int a = (b > 10) ? some_value                 
: another_value;

What's the syntax for nested conditional statements?

An else if statement must always correspond to a beginning if statement on the same "nesting level".

Your inner else if does not have any beginning if statement and it therefore fails.

if ($AD == 1) {
if ($AC == '') {
echo 'empty';
}
}
else {
otherFunction($AD);
}

Since this is basic PHP (actually IFs are mostly not language-dependent) stuff, I recommend you reading a beginner's tutorial.

PHP Elvis operator vs null coalescing operator

When your first argument is null, they're basically the same except that the null coalescing won't output an E_NOTICE when you have an undefined variable. The PHP 7.0 migration docs has this to say:

The null coalescing operator (??) has been added as syntactic sugar
for the common case of needing to use a ternary in conjunction with
isset(). It returns its first operand if it exists and is not NULL;
otherwise it returns its second operand.

Here's some example code to demonstrate this:

<?php

$a = null;

print $a ?? 'b'; // b
print "\n";

print $a ?: 'b'; // b
print "\n";

print $c ?? 'a'; // a
print "\n";

print $c ?: 'a'; // Notice: Undefined variable: c in /in/apAIb on line 14
print "\n";

$b = array('a' => null);

print $b['a'] ?? 'd'; // d
print "\n";

print $b['a'] ?: 'd'; // d
print "\n";

print $b['c'] ?? 'e'; // e
print "\n";

print $b['c'] ?: 'e'; // Notice: Undefined index: c in /in/apAIb on line 33
print "\n";

The lines that have the notice are the ones where I'm using the shorthand ternary operator as opposed to the null coalescing operator. However, even with the notice, PHP will give the same response back.

Execute the code: https://3v4l.org/McavC

Of course, this is always assuming the first argument is null. Once it's no longer null, then you end up with differences in that the ?? operator would always return the first argument while the ?: shorthand would only if the first argument was truthy, and that relies on how PHP would type-cast things to a boolean.

So:

$a = false ?? 'f'; // false
$b = false ?: 'g'; // 'g'

would then have $a be equal to false and $b equal to 'g'.

How to write ternary conditional operator?

In Kotlin, if statements are expressions. So the following code is equivalent:

if (a) b else c

The distinction between expression and statement is important here. In Java/C#/JavaScript, if forms a statement, meaning that it does not resolve to a value. More concretely, you can't assign it to a variable.

// Valid Kotlin, but invalid Java/C#/JavaScript
var v = if (a) b else c

If you're coming from a language where if is a statement, this might seem unnatural but that feeling should soon subside.



Related Topics



Leave a reply



Submit