$GLOBALS can only be modified using the $GLOBALS[$name] = $value syntax

Description

Since PHP 8.1, it is not possible to replace $GLOBALS entirely. It has to be modified at the index level, and not as a whole.

The error message is a bit misleading: on the spot operations with index are still possible, such as ++ or array_map or array_walk.

This means that $GLOBALS['index'] may be written, while $GLOBALS cannot.

$GLOBALS['index'] and $GLOBALS can still be used for reading.

Example

<?php

$GLOBALS = [];

// That is also OK
$x = 3;
echo $GLOBALS['x']++;

?>

Solutions

  • Make a loop over $GLOBALS and update each index individually.

  • Avoid updating $GLOBALS: keep a copy in another variable.