note 123038 deleted from language.operators.comparison by cmb
| From: | cmb@php.net | Date: | Sun, 08 Dec 2019 11:34:45 +0000 |
| Subject: | note 123038 deleted from language.operators.comparison by cmb | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-213471@lists.php.net to get a copy of this message | ||
Note Submitter: phpnotes at zavaboy dot com
----
A note on null coalescing operators with arithmetic as it may not be obvious for some people.
<?php
// Only $a and $c are defined as:
$a = 4; $c = 2;
// When you do this:
echo $a ?? 0 + $b ?? 0 + $c ?? 0 ; // 4
// You may have expected a result like this:
echo ($a ?? 0)+($b ?? 0)+($c ?? 0); // 6
// But this is basically what's going on:
echo $a ??(0 + $b)??(0 + $c)?? 0 ; // 4
?>
Also note the first and last will produce an E_NOTICE if $a is not defined or null and $b is not
defined:
Notice: Undefined variable: b in __FILE__ on line __LINE__
See also:
http://php.net/manual/en/language.operators.precedence.php