PHP Superglobal Variables

A global variable is a variable with global scope, meaning that it is visible throughout the program. The set of all global variables is known as the global environment or global state. In PHP, the superglobal variable do not need to be declared, nor do they need to be marked with global in functions. Superglobals were introduced in PHP 4.1.0.

The PHP superglobal variables are -

  • $_SERVER
  • $_POST
  • $_GET
  • $_ENV
  • $_FILES
  • $_COOKIE

List of Superglobal variables

These are the following superglobal variables.


The $_GET super global variable is used to collect data from the form. It converts the name/value of form data in a query string and append to the requested page. The data sent from a form with the GET method is visible to everyone and it also has limits on the amount of information to send. The limitation of data is about 2000 characters.

As, all variable names and values are displayed in the URL, it may be used for sending non-sensitive data.

Example -

The $_GET[] array retrieves variables from the query string or URL.
Like - $_GET['name'] contains value 'john' and $_GET['age'] contains value 32.

As, the GET method is visible to everyone in the URL so this is not secure and not preferred to send sensitive information.

        $name = $_GET['name'];
        $age = $_GET['age'];
        echo "<h1>My name is ".$name.". I am ".$age." years old.</h1><br>";


In PHP, the $_POST superglobal variable is used to collect form data after submitting an HTML form with method="post". This variable embedded the form name/value data and sent in HTTP headers. Data sent from a form with the POST method is undetectable and has no limits on the amount of data to send. That's why it is more secure than the GET method.

Example -

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

  // collect form data
  if( $_POST["fname"] || $_POST["age"] ) {
	echo "Hello ". $_POST['fname']. "<br />";
	echo "You are ". $_POST['age']. " years old.";


Cookie is used to make the data persistent across request, which means we can use the data store in cookie across different pages. A cookie is used to identify a user.
The setcookie() function is used to set the data and store it in the browser, and the $_COOKIE superglobal variable is used to get the previously set cookie values.

Example -

	setcookie('username', $username, time()+20000, '/');
if (isset($_COOKIE['username'])) {
    echo "The username is ".$_COOKIE['username'];

In the above line, username is the name of cookie to be set, the $username has the value of the username cookie and time()+20000 sets the expiration time.


A PHP session stores data on the server rather than user's computer.
In PHP, the session_start() function is used to create a client session and generates a session id. The $_SESSION holds session variables, that can be accessed on multiple pages.

Example -

$_SESSION['userid'] = '122';
$_SESSION['department'] = 'software';


In PHP, the $_SERVER is a PHP super global variable which holds information about headers, paths, and script locations. It stores the information about the web server and the execution environment. The entries in this array are created by the web server. The keys to the values in this array are predefined.

Example -

echo $_SERVER['SERVER_NAME']."<br>";
echo $_SERVER['HTTP_HOST']."<br>";
echo $_SERVER['PHP_SELF']."<br>";

Output -

Read more articles

General Knowledge

Learn Popular Language