I’m reading the PHP documentation for the $GLOBALS array, which says:
As of PHP 8.1.0, write access to the entire $GLOBALS array is no
longer supported
It also says:
As of PHP 8.1.0, $GLOBALS is now a read-only copy of the global symbol
table. That is, global variables cannot be modified via its copy.
Previously, $GLOBALS array is excluded from the usual by-value
behavior of PHP arrays and global variables can be modified via its
copy.
I’m not sure what the above quotes mean, do they mean that I can’t do the following?:
<?php
$x = "hello";
$GLOBALS["x"] = "bye";
echo $x;
?>
2
Answers
Prior to PHP version
8.1.0
you could assign a value to the entire$GLOBALS
array (3v4l):In every more recent PHP version, you will get a fatal error (3v4l):
As the error message suggests, you may still assign a value using the array syntax.
This is a consequence of the Restrict $GLOBALS usage RFC.
The proposal states:
and later explicitly confirms that
$GLOBALS['x'] = 1;
should continue to work. However, it is now a special case, and$GLOBALS
can no longer, in general, be used with operations that would modify it in place: