🔥 $100K Hit! Where Will Bitcoin Go Next? Find Out Live!

Code has been added to clipboard!

PHP Superglobals: The Next Level of PHP Global Variable Scope

Reading time 5 min
Published Aug 8, 2017
Updated Oct 2, 2019

One of the most important details about PHP variables is called scope. It defines the location where a particular variable can be used: a PHP global variable can be applied anywhere outside the function, and the one of a local scope can only be used in the particular method it was first declared in.

However, there is one more type of variables. They are called PHP superglobals and can be accessed in any scope, file, class or function.

As for their data type, PHP superglobals are PHP arrays that contain data of external origin (for example, information from cookies or forms). That is precisely why you don't need a particular code to access them. In this tutorial, we will introduce the ones that are used most often, and present you with some examples.

PHP Superglobals: Main Tips

  • Superglobals do not have a fixed scope: they are available in all of them. Therefore, they differ from local and global valuables.
  • This type of variables have been around since PHP 4.1.

The Most Common Superglobals

As we have mentioned, some PHP superglobals are used more than others. Take a look at a handful of those you are most likely to find useful in your daily tasks:

  • $GLOBALS
  • $_SERVER
  • $_REQUEST
  • $_POST
  • $_GET
  • $_SESSION
  • $_COOKIE
  • $_FILES
  • $_ENV

We will now have a closer look at the first five, using examples.

$GLOBALS

This variable is a PHP superglobal needed to gain access to PHP variables arising out of any location script-wise.

In this example, a superglobal $GLOBALS is used to access PHP variables needed to find out the $z:

Example
<?php
$x = 50; 
$y = 5;
 
function addition() { 
    $GLOBALS['z'] = $GLOBALS['x'] + $GLOBALS['y']; 
}
 
addition(); 
echo $z; 
?>

Warning: the code above should not be replicated as this approach is not correct. It is an example of what not to do in PHP.

$_SERVER

This variable contains data recalling paths, headers and locations of scripts.

In the code below, you can see the usage of a couple of elements in this variable:

Example
<?php
echo $_SERVER['PHP_SELF'];
echo "<br>";
echo $_SERVER['SERVER_NAME'];
echo "<br>";
echo $_SERVER['HTTP_HOST'];
echo "<br>";
echo $_SERVER['HTTP_REFERER'];
echo "<br>";
echo $_SERVER['HTTP_USER_AGENT'];
echo "<br>";
echo $_SERVER['SCRIPT_NAME'];
?>

This table shows the key elements of the PHP $_SERVER variable:

Element/Code Description
$_SERVER['PHP_SELF'] Used to make PHP get present script filename
$_SERVER['GATEWAY_INTERFACE'] Used to make PHP get the number of CGI (Common Gateway Interface) version being used
$_SERVER['SERVER_ADDR'] Used to make PHP get host server IP address
$_SERVER['SERVER_NAME'] Used to make PHP get a name
$_SERVER['SERVER_SOFTWARE'] Used to make PHP get an ID string is returned (Apache/2.1.10)
$_SERVER['SERVER_PROTOCOL'] Used to make PHP get the revision and name of the data protocol (HTTP/1.0)
$_SERVER['REQUEST_METHOD'] Used to make PHP get a request for the page access (POST, GET)
$_SERVER['REQUEST_TIME'] Used to make PHP get a timestamp of the start of the request (1234568790)
$_SERVER['QUERY_STRING'] Used to make PHP get a string of query
$_SERVER['HTTP_ACCEPT'] Used to make PHP get an accept header from the present request.
$_SERVER['HTTP_ACCEPT_CHARSET'] Used to make PHP get an accept_charset header (utf-8, ISO-8789-2)
$_SERVER['HTTP_HOST'] Used to make PHP get a host header
$_SERVER['HTTP_REFERER'] Used to make PHP get the full URL of the present page visited. Not very reliable, not fully supported by all of the user-agents either.
$_SERVER['HTTPS'] A script that is queried to a HTTP protocol (Secure)
$_SERVER['REMOTE_ADDR'] Used to make PHP get the user side page IP address
$_SERVER['REMOTE_HOST'] Used to make PHP get the host name
$_SERVER['REMOTE_PORT'] Used to make PHP get the user's machine port
$_SERVER['SCRIPT_FILENAME'] Used to make PHP get an absolute pathname
$_SERVER['SERVER_ADMIN'] Used to make PHP get a SERVER_ADMIN given value
$_SERVER['SERVER_PORT'] Used to make PHP get a port of the server machine
$_SERVER['SERVER_SIGNATURE'] Used to make PHP get a virtual host name and server version
$_SERVER['PATH_TRANSLATED'] Used to make PHP get the path of the file system
$_SERVER['SCRIPT_NAME'] Used to make PHP get a path
$_SERVER['SCRIPT_URI'] Used to make PHP get an URI
DataCamp
Pros
  • Easy to use with a learn-by-doing approach
  • Offers quality content
  • Gamified in-browser coding experience
  • The price matches the quality
  • Suitable for learners ranging from beginner to advanced
Main Features
  • Free certificates of completion
  • Focused on data science skills
  • Flexible learning timetable
Udacity
Pros
  • Simplistic design (no unnecessary information)
  • High-quality courses (even the free ones)
  • Variety of features
Main Features
  • Nanodegree programs
  • Suitable for enterprises
  • Paid Certificates of completion
edX
Pros
  • A wide range of learning programs
  • University-level courses
  • Easy to navigate
  • Verified certificates
  • Free learning track available
Main Features
  • University-level courses
  • Suitable for enterprises
  • Verified certificates of completion

$_REQUEST

This superglobal is needed in order to receive data from a submitted HTML form.

Example
<form method="post" action="<?php echo $_SERVER['PHP_SELF'];?>">
  Name: <input type="text" name="fname">
  <input type="submit">
</form>

<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
    // collect value of input field
    $name = $_REQUEST['fname'];
    if (empty($name)) {
        echo "Name is empty";
    } else {
        echo $name;
    }
}
?>

$_POST

$_POST superglobal is needed in order to pass PHP variables or collect data from forms after they have been submitted using the method="post" via an HTML form.

Below you can see a form with a Submit button and an input field. The data is submitted when the button is clicked and then sent to the file stated in the action attribute of the <form> tag. In our case, we send the data to the file itself. You can use any other PHP file to take in the form data by changing the name of the file.

The last step is to use the superglobal $_POST for collecting the data inside the input field:

Example
<html>
<body>

<form method="post" action="<?php echo $_SERVER['PHP_SELF'];?>">
  Name: <input type="text" name="fname">
  <input type="submit">
</form>

<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
    // collect value of input field
    $name = $_POST['fname'];
    if (empty($name)) {
        echo "Name is empty";
    } else {
        echo $name;
    }
}
?>

</body>
</html>

$_GET

$_GET is needed in order to gather data from forms with the method="get" and data sent via the URL.

Presume there is an HTML page with a hyperlink:

Example
<html>
<body>

<a href="learn_get.php?subject=PHP&web=learn.xyz">Test $GET</a>

</body>
</html>

After the Test $GET link is clicked, the following parameters (web and subject) will be passed to learn_get.php. Using $_GET, the values inside learn_get.php can be accessed.

In the below example you can see the code inside learn_get.php:

Example
<html>
<body>

<?php 
echo "Learn " . $_GET['subject'] . " at " . $_GET['web'];
?>

</body>
</html>

PHP Superglobals: Summary

  • PHP arrays that are called superglobals hold data that originated from external sources and can be accessed in all scopes.
  • PHP 4.1.0 was the first version to support superglobals.