note 126395 added to language.operators.comparison
| From: | nullboi at main dot php dot net | Date: | Tue, 07 Sep 2021 02:11:04 +0000 |
| Subject: | note 126395 added to language.operators.comparison | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-216921@lists.php.net to get a copy of this message | ||
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'
?>
----
Server IP: 45.112.84.4
Probable Submitter: 45.112.85.242 (proxied: 89.238.150.174)
----
Manual Page -- https://php.net/manual/en/language.operators.comparison.php
Edit -- https://main.php.net/note/edit/126395
Del: integrated -- https://main.php.net/note/delete/126395/integrated
Del: useless -- https://main.php.net/note/delete/126395/useless
Del: bad code -- https://main.php.net/note/delete/126395/bad+code
Del: spam -- https://main.php.net/note/delete/126395/spam
Del: non-english -- https://main.php.net/note/delete/126395/non-english
Del: in docs -- https://main.php.net/note/delete/126395/in+docs
Del: other reasons-- https://main.php.net/note/delete/126395
Reject -- https://main.php.net/note/reject/126395
Search -- https://main.php.net/manage/user-notes.php