note 126395 deleted from language.operators.comparison by salathe
| From: | salathe@php.net | Date: | Tue, 07 Sep 2021 07:25:14 +0000 |
| Subject: | note 126395 deleted from language.operators.comparison by salathe | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-216922@lists.php.net to get a copy of this message | ||
Note Submitter: nullboi
----
Not mentioned in this document regarding the Null Coalescing Operator is that you can use is as an
assigning operator (??=) and works nicely.
<?php
echo $test;
// PHP Warning: Undefined variable $test
echo $test ?? 'foo';
// prints 'foo' without error
// $test remains undefined, echo $test still throw a PHP warning
$test ??= 'bar';
// $test is undefined therefore equals NULL
// no error, $test is now set to 'bar'
echo $test ?? 'baz';
// $test is no longer undefined/NULL, prints 'bar' without error
$test ??= 'baz';
// $test is no longer undefined/NULL, $test is not changed
echo $test;
// prints 'bar'
?>