Global Variables – Superglobals Explained
What are Global Variables?
A global variable in PHP is a variable that is accessible from all the functions or methods in the PHP script. By default, variables defined outside of functions are not accessible within a function’s scope. If you want a function to access a variable that’s defined in the global scope, you use the global keyword.
Also checkout our various PHP Tutorial Videos.
Here is a basic example:
$x = 10; // global variable
function myFunc() {
global $x;
echo $x; // outputs 10
}
myFunc();
echo PHP_EOL;
$y = 5; // global variable
function myFunc2() {
return $y = 8; // local variable
}
echo $y; // outputs 5 since it's referencing the global variable
echo PHP_EOL;
echo myFunc2(); // outputs 8 since it's referencing the local variable in the function
In the example above, the $x variable is a global variable. When we use the global keyword before the variable $x inside the function, we are telling PHP that we want to use the global version of the variable, not a local one.
In the second example from above, we create a global variable $y and set the value to 5. We then create a second function called myFunc2() and inside we create another $y variable which is local to the function and set the value to 8.
Outside of the function, we then echo $y and see that it is outputting the value 5 which is the global variable. We then echo out myFunc2() and it outputs 8 which is the value of the local variable inside the function.
What are Superglobals
Superglobals in PHP are predefined variables, which are always accessible, regardless of scope. They can be accessed from any function, class or file without having to do anything special, like using the global keyword.
Learn more about PHP Functions.
Here are the superglobal variables in PHP:
- $GLOBALS
- $_SERVER
- $_GET
- $_POST
- $_FILES
- $_COOKIE
- $_SESSION
- $_REQUEST
- $_ENV
Here is a brief explanation of each:
$GLOBALS: An associative array referencing all variables which are currently defined in the global scope.
$_SERVER: This is an array containing server variables created by the web server.
$_GET: This is an associative array of variables passed to the current script via the URL parameters.
$_POST: An associative array of variables passed to the current script via the HTTP POST method.
$_FILES: An associative array of items uploaded to the current script via the HTTP POST method.
$_COOKIE: An associative array of variables passed to the current script via HTTP cookies.
$_SESSION: An associative array containing session variables available to the current script.
$_REQUEST: An associative array that by default contains the contents of $_GET, $_POST and $_COOKIE.
$_ENV: An associative array of variables passed to the current script via the environment method.
Here are some examples of how to use these superglobal variables:
$GET
// Assuming URL is: http://example.com?name=Joel
echo 'Hello, ' . htmlspecialchars($_GET["name"]) . '!';
// Outputs: Hello, Joel!
$_POST
// Assuming a form posted with a field named 'username'
echo 'Hello, ' . htmlspecialchars($_POST["username"]) . '!';
// Outputs: Hello, {username}!
$_SESSION
// Starting the session
session_start();
// Setting a session variable
$_SESSION["username"] = "Joel";
// Accessing the session variable
echo 'Hello, ' . $_SESSION["username"] . '!';
// Outputs: Hello, Joel!
$_COOKIE
// Setting a cookie
setcookie("username", "Joel", time() + (86400 * 30), "/"); // 86400 = 1 day
// Accessing the cookie
if(!isset($_COOKIE["username"])) {
echo "Cookie named 'username' is not set!";
} else {
echo "Cookie 'username' is set!<br>";
echo "Value is: " . $_COOKIE["username"];
// Outputs: Value is: Joel
}
$_SERVER
echo $_SERVER['SERVER_NAME'];
// Outputs: the server's hostname
$GLOBALS
$x = 65;
$y = 35;
function add() {
$GLOBALS['z'] = $GLOBALS['x'] + $GLOBALS['y'];
}
add();
echo $z; // Outputs: 100
In the $GLOBALS example, $z is accessible globally and within the function to access $x and $y.
Remember to always sanitize any user-provided data when dealing with superglobals to prevent security vulnerabilities like XSS attacks or SQL injection.
Make sure to checkout our guide on PHP Variables.