I've just seen a video about upcoming PHP 7.4 features and saw this new ??=
operator. I already know the ??
operator.
How's this different?
From the docs:
Coalesce equal or ??=operator is an assignment operator. If the left parameter is null, assigns the value of the right paramater to the left one. If the value is not null, nothing is done.
Example:
// The folloving lines are doing the same
$this->request->data['comments']['user_id'] = $this->request->data['comments']['user_id'] ?? 'value';
// Instead of repeating variables with long names, the equal coalesce operator is used
$this->request->data['comments']['user_id'] ??= 'value';
So it's basically just a shorthand to assign a value if it hasn't been assigned before.
The null coalescing assignment operator is a shorthand way of assigning the result of the null coalescing operator.
An example from the official release notes:
$array['key'] ??= computeDefault();
// is roughly equivalent to
if (!isset($array['key'])) {
$array['key'] = computeDefault();
}
Null coalescing assignment operator chaining:
$a = null;
$b = null;
$c = 'c';
$a ??= $b ??= $c;
print $b; // c
print $a; // c
Example Docs:
$array['key'] ??= computeDefault();
// is roughly equivalent to
if (!isset($array['key'])) {
$array['key'] = computeDefault();
}
You can use this to initialize variables during a loop's first iteration. But beware!
$reverse_values = array();
$array = ['a','b','c']; // with [NULL, 'b', 'c'], $first_value === 'b'
foreach($array as $key => $value) {
$first_value ??= $value; // won't be overwritten on next iteration (unless 1st value is NULL!)
$counter ??= 0; // initialize counter
$counter++;
array_unshift($reverse_values,$value);
}
// $first_value === 'a', or 'b' if first value is NULL
// $counter === 3
// $reverse_values = array('c','b','a'), or array('c','b',NULL) if first value is null
If the first value is NULL
, then $first_value
will be initialized to NULL
and then overwritten by the next non-NULL
value. If the array has a lot of NULL
values, $first_value
will end up either as NULL
or the first non-NULL
after the last NULL
. So this seems like a terrible idea.
I would still prefer doing something like this mainly because it's more clear, but also because it works with NULL
as an array value:
$reverse_values = array();
$array = ['a','b','c']; // with [NULL, 'b', 'c'], $first_value === NULL
$counter = 0;
foreach($array as $key => $value) {
$counter++;
if($counter === 1) $first_value = $value; // does work with NULL first value
array_unshift($reverse_values,$value);
}
It can roughly be translated as "$a defaults to $b", like this:
$page ??= 1; // If page is not specified, start at the beginning
$menu ??= "main"; // Default menu is the main menu
$name ??= "John Doe"; // Name not given --> use John Doe
A long-awaited tool in the world of PHP.
Before PHP 7.4, we did this with a function:
function defaultOf(&$var, $value) {
if(is_null($var)) $var=$value;
}
// Now the 3 statements above would look like:
defaultOf( $page, 1 );
defaultOf( $menu, "main" );
defaultOf( $name, "John Doe" );
(I still use it because it's more readable.)