PHP $GLOBALSĀ is a super global variable. It references all variables available in the global scope. i.e. It can access anywhere in PHP script and also within PHP functions or methods. It always defines outside of function or class.
Global variables are often used to store data that needs to be accessed and updated throughout the execution of the script.
In a large project, global variables may cause issues with the readability and maintainability of the code.
It stores all global variables in an array. i.e. $GLOBALS[index]
Example:-
<?php
$name = “KSH”;
function globalReferance(){
$name = “TUOR”;
echo ‘$name in global’ . $GLOBALS[‘name’];
echo ‘ $name in local’ . $name;
}
globalReferance();
?>
Output”-
$name in global KSH
$name in local TUTOR
Note:-
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, the $GLOBALS array is excluded from the usual by-value behavior of PHP arrays and global variables can be modified via its copy.
Read Also:-
For more detail about variables please see manual