$GLOBALS is a built-in PHP superglobal associative array that stores all global variables in a PHP script. It provides a way to access global variables from any scope, including inside functions, classes, or methods, without needing to pass them as arguments or use the global keyword.
To access a global variable within a function, you must either declare it as global using the global keyword or reference it through the $GLOBALS array.
How Does $GLOBALS Work?
Here is a simple example to illustrate why $GLOBALS is needed:
PHP <?php $message = "Hello, World!"; function printMessage() { echo $message; // Undefined variable error } printMessage(); ?>
Notice: Undefined variable: message
Inside printMessage(), $message is undefined because the function has its own local scope.
Now, using $GLOBALS:
PHP <?php $message = "Hello, World!"; function printMessage() { echo $GLOBALS['message']; // Access global variable directly } printMessage(); ?>
The function accesses the global variable $message through $GLOBALS['message'].
Accessing and Modifying Global Variables Using $GLOBALS
You can not only read but also change global variables inside functions via $GLOBALS.
PHP <?php $count = 5; function incrementCount() { $GLOBALS['count']++; } incrementCount(); incrementCount(); echo $count; // Output: 7 ?>
Here, the function increments the global $count by modifying it inside $GLOBALS.
When to Use $GLOBALS
- When you need to access or modify global variables inside functions without declaring each one as global.
- When dealing with many global variables and you want a consistent, readable approach.
- Useful in procedural code or legacy scripts where passing parameters is impractical.
Best Practices for Using $GLOBALS
- Limit usage to cases where other alternatives (function parameters, class properties) are impractical.
- Avoid global state whenever possible; encapsulate variables within functions or classes.
- Use $GLOBALS primarily for small scripts or debugging.
- Document variables accessed via $GLOBALS are clearly for maintainability.
Similar Reads
PHP Constants A constant is a name or identifier used to store a fixed value that does not change during the execution of a PHP script. Unlike variables, constants do not start with a $ symbol and stay the same once they are defined.Constants are immutable (cannot be changed after definition).They are global by d
3 min read
PHP | Superglobals PHP superglobals are predefined variables that are globally available in all scopes. They are used to handle different types of data, such as input data, server data, session data, and more. These superglobal arrays allow developers to easily work with these global data structures without the need t
6 min read
What is the use of $GLOBALS in PHP ? In this article, we will discuss $GLOBALS in PHP. $GLOBALS is a superglobal variable used to access global variables from anywhere in the PHP program. PHP stores all global variables in an array called $GLOBALS[index]. Syntax: $GLOBALS['index']=value;value is the input value.The index is the unique
1 min read
PHP Namespace A namespace in PHP is a container for logically grouping classes, interfaces, functions, and constants. They help avoid name collisions by allowing the same name to be used in different namespaces without conflict. Using namespaces, you can make your code more organized, maintainable, and scalable.A
4 min read
PHP File Handling In PHP, File handling is the process of interacting with files on the server, such as reading files, writing to a file, creating new files, or deleting existing ones. File handling is essential for applications that require the storage and retrieval of data, such as logging systems, user-generated c
4 min read