PHP Superglobals

In this PHP tutorial you will learn all about the Superglobals in PHP. We will discuss in detail about the Global Variables and various types of superglobal variables $GLOBALS, $_SERVER, $_REQUEST, $_POST, $_GET, $_FILES, $_ENV, $_COOKIE, $_SESSION.

What are Superglobal variables in PHP?

In PHP, superglobal variables are built-in predefined global variables. The superglobals can be found throughout the script. These variables may be accessible from any function, class, or file without the need for any extra actions, such as defining any global variables, and so on. They are mostly used in applications to store and retrieve information from one page to another.

Various Superglobal variables in PHP

1.    $GLOBALS
2.    $_SERVER
3.    $_REQUEST
4.    $_GET
5.    $_POST
6.    $_SESSION
7.    $_COOKIE
8.    $_FILES

What is $GLOBALS in PHP?

$GLOBALS in PHP is a superglobal variable that may be accessed from anywhere in the script. PHP keeps all global variables in the array $GLOBALS[], where the index carries the name of the global variable that may be accessed.


$x = 15;
$y = 53; 
function sum() {
  $GLOBALS['z'] = $GLOBALS['x'] + $GLOBALS['y'];
}
sum();
echo $z;

Output:


68

What is $_SERVER in PHP?

$_SERVER is a superglobal variable in PHP that contains information about headers, paths, and script locations. Some of these components are used to retrieve data from the global variable $_SERVER.

  •  $_SERVER[‘PHP_SELF’] - it is used to return the file name of the script that is currently being executed.         
  • $_SERVER[‘SERVER_NAME’] - it is used to return the name of the server that is hosting the site.
  • $_SERVER[‘HTTP_HOST’] - it is used to return the header of the host of the current request.
  • $_SERVER[‘SCRIPT_NAME’] - it is used to return the current script’s path.

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'];

Output:


/files/new/server.php
localhost
localhost
http://localhost/files/new/
Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/96.0.4664.45 Safari/537.36
/files/new/server.php

What is $_REQUEST in PHP?

$_REQUEST is a superglobal variable that is used to collect data once an HTML form on submission. $_REQUEST is rarely used since $_POST and $_GET provide the same function and are more commonly used.


<!DOCTYPE html>
<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") {
    $name = $_REQUEST['fName'];
    if (empty($name)) {
        echo "Name field is empty";
    } else {
        echo " The entered name is $name";
    }
}
?>
</body>
</html>

Output:

PHP - For Loop

What is $_GET in PHP?

$_GET is a superglobal variable that is used to collect data from the HTML form after it has been submitted. When a form uses the get method to send data, the data is available in the query string, therefore the values are not concealed. The superglobal array variable $_GET contains the values coming through the URL.


<!DOCTYPE html>
<html>
<body> 
<form method="get" action="getsup.php">
  Name: <input type="text" name="fName">
  <input type="submit" name="submit">
</form>
<?php
if (isset($_GET['submit'])) {
    $name = $_GET['fName'];
    echo " The entered name is $name";
}
?>
</body>
</html>

Output:


PHP - For Loop

What is $_POST in PHP?

$_POST is a superglobal variable that is used to collect data from the HTML form after it has been submitted. Because the data is not accessible in the query string when a form implements the method post to transfer data, security standards are maintained in this manner.


<!DOCTYPE html>
<html>
<body>
<form method="post">
  Name: <input type="text" name="fName">
  <input type="submit" name="submit">
</form>
<?php
if (isset($_POST['submit'])) {
    $name = $_POST['fName'];
    echo " The entered name is $name";
}
?>
</body>
</html>

Output:


PHP - For Loop

What is $_SESSION in PHP?

A $_SESSION variable is a PHP superglobal that keeps and provides information about a site user from the moment the site is opened until it is closed. A session is initiated each time a person visits a website. The value in the session can be accessed in multiple pages of the website.
To start a session we use the function session_start()
To end the session we use the function session_destroy()

What is $_COOKIE in PHP?

A cookie is a tiny file that a server places on a user's computer. It establishes the user's identity. Whenever a server request is made. Typically, a cookie is sent together with the request. The setcookie() function in PHP is used to create cookies.  


<?php
    $cookie_name = "username";
    $cookie_value = "John";
    setcookie($cookie_name, $cookie_value, time()+(86400*30),"/");
?>    
    

Output:


PHP - For Loop

What are $_FILES in PHP?

$_FILES is a variable in PHP that is used to store objects uploaded using the HTTP POST method. The keys of this array are the names of the fields uploading the files and accessing the data. $_FILES[fileUploaded][name], for example, gets the name of the file being uploaded from the file uploaded property.  
The $_FILES has multiple elements, which are listed below:

  •     $_FILES[‘file’][‘name’] - It is used to hold the original name of file to be uploaded.
  •     $_FILES[‘file’][‘type’] - It is to refers to the type of the file being uploaded.
  •    $_FILES[‘file’][‘size’] - It contains the file size in bytes.
  •    $_FILES[‘file’][‘tmp_name’] - It is to refers to a temporary filename of the storage file uploaded on the server.
  •    $_FILE[‘file’][‘error’] - It holds the file upload’s associated error code.