Null Coalescing Assignment Operator throwing parse / syntax error in PHP 8.0

429 Views Asked by At

I'm transitioning from 7.3 to PHP 8.0 (centOS) and going through the fun process of patching hordes of undefined array keys. It's easy, but time consuming. One new tool offered by PHP I hoped would offset this burden is the new Null Coalescing Assignment Operator ??= added in 7.4

https://www.php.net/manual/en/migration74.new-features.php#migration74.new-features.core.null-coalescing-assignment-operator

However, any use of this operator is throwing a parse error:

PHP Parse error:  syntax error, unexpected '=' in /home...

Example Code (based on PHP example linked above):

function foo(){
   return 'bar';
}

$array['key'] ??= foo();

Anyone run into this / know what I'm missing?

1

There are 1 best solutions below

0
On

Got it. Edge-case, but in case anyone else runs into this issue on a multiPHP server, I realized the error was only being thrown when the script was cronned. The cron job command was pointing to the general PHP binary /usr/local/bin/php.

Pointing to the specific PHP8 binary /opt/cpanel/ea-php80/root/bin/php in the command did the trick.