PHP Superglobals Explained

Last Updated: 06 Jan, 2024

What are PHP superglobal variables?

Superglobal variables are built-in global variables that are always available in all scopes. The meaning is they are available in all scopes throughout a script. You no need to use global keyword to access them within functions or methods.

Below are the list of superglobal variables:

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

$GLOBALS superglobal variable

It helps to access global variables from anywhere in the PHP script. In PHP, all global variables are stored in an array called $GLOBALS[]. Index of this variable holds the name of global variable that can be accessed.

Sample Example:

<?php
$abc = 100;
$xyz = 200;

function calculateSum() {
	$temp = $GLOBALS['abc'] + $GLOBALS['xyz'];
	$GLOBALS['sum'] = $temp;
}

calculateSum();
echo $sum;

Output:

300

$_SERVER superglobal variable

This super global variable containes information like headers, paths, and script locations. All the entries inside $_SERVER array are created by the web server. Below are the list of mostly used informations from this variable:

  • PHP_SELF: Returns the filename of the script that is currently being executed, relative to the document root.
  • SERVER_NAME: Returns the name of the host server (such as www.w3techpoint.com).
  • SERVER_ADDR: Returns the host server IP Address.
  • HTTP_HOST: Returns the contents of the Host: header from the current request, if there is one.
  • REQUEST_METHOD: Returns the request method that is used to access the page (such as GET or POST).
  • REQUEST_URI: Returns the URI which was given in order to access this page (such as ‘/index.html’).
  • QUERY_STRING: Returns the query string, if any, via which the page was accessed.
  • SCRIPT_NAME: Returns the contents of the current script’s path. This is useful for the pages that need to point to themselves.

Sample Example:

<?php
$temp = array(
	'filename' =--> $_SERVER['PHP_SELF'], 
	'server_name' => $_SERVER['SERVER_NAME'],
	'server_address' => $_SERVER['SERVER_ADDR'],
	'host_name' => $_SERVER['HTTP_HOST'],
	'request_method' => $_SERVER['REQUEST_METHOD'],
	'request_uri' => $_SERVER['REQUEST_URI'],
	'query_string' => $_SERVER['QUERY_STRING'],
	'script_name' => $_SERVER['SCRIPT_NAME'],
	'script_uri' => $_SERVER['SCRIPT_URI']
);
print_r($temp);

Output:

Array
(
    [filename] => /index.php
    [server_name] => www.wtechpoint.com
    [server_address] => 127.0.0.1
    [host_name] => www.wtechpoint.com
    [request_method] => GET
    [request_uri] => /?q=php
    [query_string] => q=php
    [script_name] => /index.php
)

$_REQUEST superglobal variable

This super global variable is used to collect data after submitting an HTML form. When a user tries to submit the form data by clicking on the submit button, the form data is sent to the file that we specify in the action attribute. $_REQUEST is used rarely, because $_POST and $_GET perform the same task and are widely used.

Sample Example:

<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") { 
    $fullname = $_REQUEST['fullname']; 
    if(empty($fullname)){ 
        echo "Your Name is empty!!"; 
    } else { 
        echo "Hi," . $fullname; 
    } 
}

$_GET superglobal variable

This super global variable is used to collect data from the HTML form. When you set form method as get and submit it, the form data is visible in the url. $_GET is an associative array of variables that is passed to the current script via the URL parameters (query string). This array is not only populated for GET requests, but rather for all requests with a query string.

Sample Example:

<?php
if ($_SERVER["REQUEST_METHOD"] == "GET") { 
    $fullname = $_GET['fullname']; 
    if(empty($fullname)){ 
        echo "Your Name is empty!!"; 
    } else { 
        echo "Hi," . $fullname; 
    } 
} 

$_POST superglobal variable

This super global variable is used to collect data from the HTML form. $_POST is an associative array of variables that are passed to the current script via the HTTP POST method when using multipart/form-data or application/x-www-form-urlencoded as the HTTP Content-Type in the request. When you set form method as post and submit it, the form data is not visible in the url and provides security.

Sample Example:

<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") { 
    $fullname = $_POST['fullname'];
    $email = $_POST['email']; 
    if(empty($fullname)){ 
        echo "Your Name is empty!!"; 
    } else { 
        echo "Hi," . $fullname; 
    } 
    if(empty($email)){ 
        echo "Your email is empty!!"; 
    } else { 
        echo "Your email is " . $email; 
    } 
} 

$_FILES superglobal variable

This super global variable is also called as HTTP File Upload variable and can be used to upload files. This is an associative array of items uploaded to the current script via the HTTP POST method. The structure of this array ($_FILES) is outlined in the uploads section of the POST method.

Sample Example:

<?php
if($_FILES[‘file’] --> 0){
 echo ‘You have selected a file to upload’;
}

$_COOKIE superglobal variable

This superglobal variable can be used to retrieve cookies. Cookie is a small text file that is loaded from a server to a client computer and stores some important information regarding the client computer. So that when the user visits the same page, necessary information can be collected from the cookie itself, decreasing the latency to open the page.

Sample Example:

<?php
setrawcookie();
print_r($_COOKIE);

$_SESSION superglobal variable

Sessions are wonderful ways to pass variables. All you need to do is start a session by session_start() function. Then you can store and access variables in and from $_SESSION. You can access it from anywhere on the server.

Sample Example:

<?php
session_start();
$_SESSION[‘name’] = ‘WTECHPOINT’;
echo $_SESSION[‘name’];

$_ENV superglobal variable

This superglobal variable can be used to return the environment variables from the web server. It is an associative array of variables passed to the current script via the environment method.

Sample Example:

<?php
echo 'My username is ' .$_ENV["USER"] . '!';

 

Thank You, Please Share.

Recommended Posts

IMAGE

PHP OOP Interfaces Explained

An Interface allows you to create programs that specifies which methods a class must implement, without defining how those methods are implemented.

IMAGE

PHP OOP Abstract Classes Explained

A PHP class that has minimum one abstract method is called as abstract class and cannot be instantiated by itself.

IMAGE

PHP Magic Methods Explained

Magic Methods are special types of methods in PHP that allows you to perform certain special actions. These methods start with a double underscore (__).