Understanding PHP Superglobals and the $_GLOBALS Variable
PHP is a popular server-side scripting language used for web development. It provides a number of superglobals, which are built-in variables that are always accessible in all scopes of a PHP script. One of these superglobals is the $GLOBALS variable, which contains a reference to all global variables defined in a PHP script.
In this article, we will dive deeper into the $GLOBALS variable and its uses in PHP programming.
What are PHP Superglobals?
Superglobals in PHP are special variables that are available in all scopes of a PHP script. This means that they can be accessed from any function, class, or file, without having to declare them as global variables.
The following are the superglobals in PHP:
$_GET: contains data passed to the script via the URL query string.$_POST: contains data passed to the script via a form submission.$_COOKIE: contains data passed to the script via cookies.$_SESSION: contains data stored in a user's session.$_REQUEST: contains data passed to the script via the URL query string, form submission, or cookies.$_SERVER: contains information about the server and environment.$_ENV: contains information about the environment.$GLOBALS: contains a reference to all global variables defined in a PHP script.
What is the $GLOBALS Variable in PHP?
The $GLOBALS variable is a superglobal that contains a reference to all global variables defined in a PHP script. This means that, with the $GLOBALS variable, you can access any global variable from anywhere in your script.
Here's an example of how the $GLOBALS variable can be used to access a global variable:
PHP example of how the $GLOBALS variable can be used to access a global variable
<?php
$x = 10;
$y = 20;
function add() {
global $x, $y;
return $x + $y;
}
echo add(); // 30
echo $GLOBALS['x']; // 10
echo $GLOBALS['y']; // 20
?>In this example, the add() function uses the global keyword to access the $x and $y variables. However, instead of using the global keyword, you can also access these variables through the $GLOBALS variable.
How to Use the $GLOBALS Variable in PHP
The $GLOBALS variable is a powerful tool in PHP programming that can be used to access global variables from any part of your script. Here are a few ways to use the $GLOBALS variable:
Accessing Global Variables
As we saw in the previous example, you can use the $GLOBALS variable to access any global variable defined in your script. Here's another example:
PHP access any global variable defined in your script
<?php
$name = "John Doe";
function greeting() {
global $name;
echo "Hello, " . $name;
}
greeting(); // Hello, John Doe
echo $GLOBALS['name']; // John Doe
?>Modifying Global Variables
The $GLOBALS variable can also be used to modify global variables. This can be useful if you need to make changes to a global variable from within a function or class.
Here's an example:
PHP modify global variables
<?php
$counter = 0;
function increment() {
global $counter;
$counter++;
}
increment();
echo $counter; // 1
echo "\n";
$GLOBALS['counter'] = 10;
echo $counter; // 10
?>In this example, the increment() function increments the value of the $counter variable. However, you can also modify the value of the $counter variable directly through the $GLOBALS variable.
Pass Variables between Functions and Classes
The $GLOBALS variable can also be used to pass variables between functions and classes. This can be useful if you need to share data between different parts of your script.
Here's an example:
PHP passing global variables between functions and classes
<?php
$data = array("name" => "John Doe", "age" => 30);
function display_data() {
global $data;
print_r($data);
}
class User {
function show_data() {
global $data;
print_r($data);
}
}
display_data(); // Array ( [name] => John Doe [age] => 30 )
$user = new User();
$user->show_data(); // Array ( [name] => John Doe [age] => 30 )
$GLOBALS['data']['email'] = "[email protected]";
display_data(); // Array ( [name] => John Doe [age] => 30 [email] => [email protected] )
$user->show_data(); // Array ( [name] => John Doe [age] => 30 [email] => [email protected] )
?>In this example, the display_data() function and the User class both access the $data variable through the $GLOBALS variable. This allows you to pass the $data variable between the different parts of your script.
Conclusion
The $GLOBALS variable is a powerful tool in PHP programming that provides access to all global variables defined in a PHP script. Whether you're accessing, modifying, or passing variables between functions and classes, the $GLOBALS variable is a versatile tool that can help you write better PHP code.
Note on best practices: While $GLOBALS is useful, relying heavily on global variables is generally discouraged in modern PHP development. Consider using dependency injection, classes, or passing variables as function arguments for better maintainability, encapsulation, and testability.
Practice
In PHP, what are the types of variables available in a global scope?