PHP Superglobal Variables

Last Updated : May 8, 2021

PHP Superglobal Variables are global scope variables; they can be accessed anywhere in PHP programs like Functions, Classes, and consecutive PHP web pages. These Super global variables are above the usual scope of PHP Variable, and we don’t have to do anything in particular to access these kinds of variables in the program. All the PHP Super global variables are an array that contains more than a single variable. Please find below the list of variables,

$GLOBALS – Superglobal Variable

$GLOBALS is a superglobal variable that holds all global scope variables, it is an array and all the variables are called by their name from this array. For example, $GLOBALS['variable_name'] returns the specific global scope variable’s value.

<?php
$a = 10;

function square() {
  $GLOBALS['square_of_a'] = $GLOBALS['a'] *  $GLOBALS['a'];
}
square();
echo $square_of_a .'<br>';
echo $GLOBALS['square_of_a'];
?>

$_SERVER – Superglobal Variable

$_SERVER is a superglobal variable that holds all the server-related information, including PHP program script’s location, PHP version, IP address, Referral address, etc. This $_SERVER variable is commonly used to check headers, current host and do the operation accordingly. Please find below the examples,

<?php
// using $_SERVER['HTTP_HOST'] variable
if($_SERVER['HTTP_HOST'] == 'www.google.com) {
  echo "It's Google!";
} else {
  echo "It's not Google";
}
// using $_SERVER['HTTP_REFERER'] varibale
if($_SERVER['HTTP_REFERER'] == 'https://www.google.com') {
  echo "It's refered by Google";
} else {
  echo "No, It's not";
}
// using $_SERVER['HTTPS'] variable
if($_SERVER['HTTPS'] !== 'off') {
  echo "HTTPS enabled";
} else {
  echo "HTTP enabled";
}
// using $_SERVER['REQUEST_METHOD'] variable
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
  echo "Requested by POST method";
}
?>

$_SERVER superglobal variables hold data that is very sensitive and important, this $_SERVER variable cannot be shared publically for security reasons because it exposes most of the information about your server and script.

$_GET – Superglobal Variable

$_GET superglobal variable contains all the values which are passed in the URL after ? symbol and holds Form data which send values through method="GET". The values sent by using the $_GET method are not secure and it has restrictions about the length of the URL. We will learn more about the Form later in this tutorial series, for now, you can get an idea from the below examples,

<form method="GET" action="action.php">
 <input type="text" name="email" value="mail@test.com"/>
 <input type="submit" value="Send"/>
</form>
<!-- or -->
<a href="action.php?email=mail@test.com">Click here!</a>

action.php

<?php
echo "<pre>"; print_r($_GET); echo "</pre>"; //prints all GET variables

echo $_GET['email'];
?>

$_POST – Superglobal Variable

$_POST superglobal variable holds the values sent via HTML Form by using method="POST". This $_POST method most widely used one for sending PHP Form values to server-side programming language. It is very secure, so all the information passed in a secure way, and the User cannot see any information in the browser. If you see the Login form on any website, then they may use this POST method to pass the form values to the server for processing. Please find below the typical example,

<form method="POST" action="action.php">
 <input type="text" name="username"/>
 <input type="text" name="password"/>
 <input type="submit" value="Send"/>
</form>

action.php

<?php
echo "<pre>"; print_r($_POST); echo "</pre>"; //prints all POST variables

echo $_POST['username'];
echo $_POST['password'];
?>

$_REQUEST – Superglobal Variable

$_REQUEST superglobal variable used to handle the data which send by both $_GET and $_POST type. If we are not sure about which method that the data are passed from the Form then we can use this $_REQUEST method to get all information for processing. Also, this $_REQUEST method not recommended to handle sensitive data instead $_POST superglobal variable is very suitable. Please find below the example,

<form method="POST" action="action.php">
 <input type="text" name="first_name"/>
 <input type="text" name="last_name"/>
 <input type="text" name="email"/>
 <input type="submit" value="Send"/>
</form>

action.php

<?php
echo "<pre>"; print_r($_REQUEST); echo "</pre>"; //prints both GET and POST variables

echo $_REQUEST['first_name'];
echo $_REQUEST['last_name'];
echo $_REQUEST['email'];
?>

$_COOKIE superglobal variable used to store/retrieve the data on the User’s local browser/system. This type of data commonly used to track the User’s activity to provide a better experience on the web application. Please find below the example,

<?php
$_COOKIE['first_time_user'] = true; //sets once login done

//later on the consecutive page
if($_COOKIE['first_time_user']) {
  echo "Welcome to PHP tutorial! We welcome you!";
}
?>

We will learn more about $_COOKIE in the later chapter of this tutorial series.

$_SESSION – Superglobal Variable

$_SESSION superglobal variable used to store information securely on the server-side and access that information across throughout web application. The data stored in $_SESSION variable available until we manually destroy them. To start a session in the application, we have to use the session_start() function then we can store all the necessary information in the $_SESSION variable to access anywhere in the application. Please find below the example,

<?php
$_SESSION['user']['logged_in'] = true; //sets once login done

// on the other page
if($_SESSION['user']['logged_in']) {
  echo "Show sensitive information that belongs to the User";
}
?>

We will learn more about $_SESSION in the later chapter of this tutorial series.

$_FILES – Superglobal Variable

$_FILES superglobal variable used when we upload files using Form elements. All the necessary information including File temporary name, File size, File Type are available in this $_FILES variable. We will learn more about $_FILES and File manipulation in the later chapter of this tutorial series. Please find below the example,

<form method="POST" action="action.php" enctype="multipart/form-data">
 <input type="text" name="first_name"/>
 <input type="text" name="last_name"/>
 <input type="file" name="file" id="file" />
 <input type="submit" value="Send"/>
</form>

action.php

<?php
echo "<pre>"; print_r($_FILES); echo "</pre>"; //prints uploaded File's information

echo $_POST['first_name'];
echo $_POST['last_name'];
?>

$_ENV – Superglobal Variable

$_ENV superglobal variable used to set environment/configuration based values and retrieve them to use and serve necessary information based on the specific environment. Please find below the example,

<?php
if($_SERVER['HTTP_HOST'] == "www.google.com") {
  $_ENV['debug_mode'] = false;
} else if($_SERVER['HTTP_HOST'] == "dev.google.com") {
  $_ENV['debug_mode'] = true;
}

if($_ENV['debug_mode']) {
   echo "Debug Mode"; die;
}
?>

In this tutorial, we learned about PHP Superglobal Variables with their example usage. We will learn more about these in the upcoming chapters.