PHP null coalesce + ternary operators strange behavior

php ternary operator
ternary operator php 7
php elvis operator
ternary operator in php w3schools
php operator
multiple ternary operator in php
php ternary without else
php ternary echo

I'm facing unexpected behavior when using new PHP7 null coalesce operator with ternary operator.

Concrete situation(dummy code):

function a()
    $a = 1;
    $b = 2;
    return $b ?? (false)?$a:$b;


The result is int(1).

Can anybody explain me why?

Your spaces do not reflect the way php evaluates the expression. Note that the ?? has a higher precedence than the ternary expression.

You get the result of:

($b ?? false) ? $a : $b;

Which is $a as long as $b is not null or evaluates to false.

Shorthand comparisons in PHP, The ternary operator is used to shorten if/else structures; The null coalescing You can read more about this strange behaviour in this Stack Overflow answer. The null coalescing operator is available since PHP 7.0. The null coalescing operator is available since PHP 7.0. It similar to the ternary operator, but will behave like isset on the lefthand operand instead of just using its boolean value. This makes this operator especially useful for arrays and assigning defaults when a variable is not set.

Examine the statement return $b ?? (false)?$a:$b;

This first evaluates $b ?? (false) whose result is then passed to ? $a:$b;

$b ?? (false) means give the first not null and isset value, which in this case is $b

Since $b = 2, which is a true-ish value, above expression becomes:

return ($b) ? $a : $b which returns value of $a which is int(1)

This whole thing will make better sense if you think of original return statement as:

return ($b ?? (false)) ? $a : $b;

We dont need to add the additional brackets because ?? is evaluated before ?

Comparison Operators - Manual, PHP's behaviour when using more than one ternary operator within a single Note: Please note that the null coalescing operator is an expression, and that it  2 PHP null coalesce + ternary operators strange behavior Oct 1 '17 2 How to load external XAML ResourceDictionary Feb 3 '12 1 .NET complex music API [closed] Dec 5 '12

return $b ?? (false)?$a:$b; // will return 1

return $b ?? ((false)?$a:$b); // will behave as you wanted

Operator Precedence - Manual, right ?? null coalescing ternary operator associativity differs from C/C++ because the behavior can change between versions of PHP or depending on the Beware the unusual order of bit-wise operators and comparison operators, this​  Operators of equal precedence that are non-associative cannot be used next to each other, for example 1 < 2 > 1 is illegal in PHP. The expression 1 <= 1 == 1 on the other hand is legal, because the == operator has lesser precedence than the <= operator.

What's The Difference Between ?? And ?: In PHP?, and the null coalescing operator ( ?? ). ?: (Elvis Operator). Since PHP 5.3+, we can use the shorter ternary operator syntax by leaving out the This has the same behavior, i.e. returning its first operand if it evaluates to true , or  If you work in a team setting, make sure the other programmers understand the code. recommends avoiding stacking ternary operators. "Is [sic] 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.".

Null coalescing operator, The null coalescing operator is a binary operator that is part of the syntax for a basic conditional expression in several programming languages, including C#, , PowerShell as of version 7.0.0, Perl as of version 5.10, Swift, and PHP 7.0.0. While its behavior differs between implementations, the null coalescing In contrast to the ternary conditional if operator used as x ? x : y  Operator Precedence. The precedence of an operator specifies how "tightly" it binds two expressions together. For example, in the expression 1 + 5 * 3, the answer is 16 and not 18 because the multiplication ("*") operator has a higher precedence than the addition ("+") operator. Parentheses may be used to force precedence, if necessary.

Twig for Template Designers - Documentation - Twig, Twig also supports a specific syntax for accessing items on PHP arrays, If a variable or attribute does not exist, you will receive a null value when the special characters (like - that would be interpreted as the minus operator): This behavior depends on the application embedding Twig. The null-coalescing operator:  [PHP-INTERNALS] [RFC] Implicit isset() in Shorthand Ternary Operator; Andrea Faulds. Sep 6, 2014 at 12:02 am: Good evening,

  • For the sake of my own sanity and any future coders using my code, I always explicitly use brackets in an expression like this. I like to state my intention and not rely on precedence or other side effects.
  • that is a good point and my bad, thank you