What is the purpose of using the superglobal array $GLOBALS in PHP?

The $GLOBALS superglobal array in PHP is used to access global variables from anywhere in the PHP script, regardless of scope. This can be useful when you need to access or modify global variables within a function or method. However, it is generally considered best practice to avoid using $GLOBALS whenever possible, as it can make code harder to maintain and debug.

// Example of using $GLOBALS to access a global variable within a function
$global_var = 10;

function access_global_var() {
    echo $GLOBALS['global_var']; // Output: 10
}

access_global_var();