note 129105 added to language.operators.assignment

From: Date: Thu, 14 Dec 2023 09:13:55 +0000
Subject: note 129105 added to language.operators.assignment
Groups: php.notes 
Request: Send a blank email to php-notes+get-221059@lists.php.net to get a copy of this message
In the case of the null coalescing assignment operator, even though the operator is right-associative just as the other assignment operators are, the left hand side is still tested first to see if it is already set or not. If it is, the right-hand side is not evaluated at all. So if you try to be clever by writing something like <?php // If $local isn't set use the $runtime value; if that's not set, // use $environment, and if that's not set, use "default"; while // setting $runtime and $environment to appropriate values // for later reference. $local ??= $runtime ??= $environment ??= "default"; ?> then the second assignment will not be evaluated if $local is already set (which means $runtime and hence $environment will be unaffected whether they are set or not). To get the code to do what it looks like it's doing, you'll have to write the assignments separately: <?php $environment ??= "default"; $runtime ??= $environment; $local ??= $runtime; ?> If in this particular instance you didn't *want* $runtime and $environment to be set to a (current or default) value, you can of course still write <?php $local ??= $runtime ?? $environment ?? "default"; ?> ---- Server IP: 45.112.84.4 Probable Submitter: 45.91.157.134 (proxied: 125.239.41.100) ---- Manual Page -- https://php.net/manual/en/language.operators.assignment.php Edit -- https://main.php.net/note/edit/129105 Del: integrated -- https://main.php.net/note/delete/129105/integrated Del: useless -- https://main.php.net/note/delete/129105/useless Del: bad code -- https://main.php.net/note/delete/129105/bad+code Del: spam -- https://main.php.net/note/delete/129105/spam Del: non-english -- https://main.php.net/note/delete/129105/non-english Del: in docs -- https://main.php.net/note/delete/129105/in+docs Del: other reasons-- https://main.php.net/note/delete/129105 Reject -- https://main.php.net/note/reject/129105 Search -- https://main.php.net/manage/user-notes.php

« previous php.notes (#221059) next »