Superglobals in PHP with Examples [2 Steps]

In this article we are going to cover Superglobals in PHP with Examples, List of Superglobal variables in PHP.

What are Superglobals in PHP?

There are so many predefined global variable in PHP. Superglobal variable in PHP are predefined global variables. Global variables are variable with global scope, which means that nor do they need to be marked with global in functions. All soperglobal variable are written in all-caps like, $_SERVER.

List of Superglobal variables in PHP:

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

1. $GLOBAL Variable in PHP:

$GLOBAL is a superglobal variable in PHP which contains all global variables in PHP script, including other superglobals. PHP stores all global variable in an array. The global variable names act as key to their values.

Example of $GLOBAL variable in PHP:

<!DOCTYPE html>
<html>
<body>

<?php 
$a = 46;
$b = 33; 

function subtraction() {
  $GLOBALS['c'] = $GLOBALS['a'] - $GLOBALS['b'];
}

subtraction();
echo $c;
?>

</body>
</html>

Output:

13
Superglobals in PHP with Examples [2 Steps] 1

2. $_SERVER Variable in PHP:

In this contains data about headers, scripts location and paths. Some of these elements are used to get the information from the superglobal variable $_SERVER.

Example of $_SERVER Variable in PHP:

<!DOCTYPE html>
<html>
<body>

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

</body>
</html>

Output:

/demo.php
localhost
localhost
Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.114 Safari/537.36
/demo.php
Superglobals in PHP with Examples [2 Steps] 2

3. $_REQUEST Variable in PHP:

This variable used to access the data after submission of HTML Form. Form method can be either ‘GET’ or ‘POST’.

Here is simple example where I have submit form which is point to itself for process with data with POST method. Now in PHP script I am using the super global variable $_REQUEST to get the value of the input field.

Example of $_ REQUEST Variable in PHP:

<!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") {
    // collect value of input field
    $name = htmlspecialchars($_REQUEST['fname']);
    if (empty($name)) {
        echo "Name is empty";
    } else {
        echo $name;
    }
}
?>

</body>
</html>

Output:

Name: Shweta
shweta
Superglobals in PHP with Examples [2 Steps] 3

4. $_POST Variable in PHP:

In this PHP $_POST is a PHP super global variable which is used to collect form data after submitting an HTML form with method=”POST”.$_POST is also widely used to pass variables.

Example of $_ POST Variable in PHP:

<!DOCTYPE html>
<html>
<body>
   
<form method="post" action="<?php echo $_SERVER['PHP_SELF'];?>">
 <label for="name">Please enter your name: </label>
 <input name="name" type="text"><br>
 <label for="age">Please enter your age: </label>
 <input name="age" type="text"><br>
 <input type="submit" value="Submit">
</form>
<?php
$nm=$_POST['name'];
$age=$_POST['age'];
echo "<strong>".$nm." is $age years old.</strong>";
?>
</body>
</html>

Output:

Please enter your name:  Shweta
Please enter your age:  23


Shweta is 23 years old.
Superglobals in PHP with Examples [2 Steps] 4

In the above code we have created a form that takes name and age of the user and accesses the data using $_POST super global variable when they submit the data. Since each superglobal variable is an array it can store more than one values. Hence we retrieved name and age from the $_POST variable and stored them in $nm and $age variables.

5. $_GET Variable in PHP:

In this $_GET super global variable is used to access form data if form is submitted with GET method. When form uses method get to transfer data, the data is visible in the query string, therefore the values are not hidden. $_GET super global array variable stores the values that come in the URL.

Example of $_ GET Variable in PHP:

<?php
   if( $_GET["name"] || $_GET["age"] ) {
      echo "Welcome ". $_GET['name']. "<br />";
      echo "You are ". $_GET['age']. " years old.";
      
      exit();
   }
?>
<html>
   <body>
   
      <form action = "<?php $_PHP_SELF ?>" method = "GET">
         Name: <input type = "text" name = "name" />
         Age: <input type = "text" name = "age" />
         <input type = "submit" />
      </form>
      
   </body>
</html>

Output:

Name: Shweta  Age:  23  

Welcome Shweta
You are 23 years old.
Superglobals in PHP with Examples [2 Steps] 5
Superglobals in PHP with Examples [2 Steps] 6

$_GET can also collect data sent in the URL.

NOTE: You will learn more about $_GET in the PHP Forms chapter.

Conclusion:

In this article we are going to cover Superglobals in PHP with Examples, List of Superglobal variables in PHP.

Related Articles:

Reference:

Superglobals in php official php page

Shweta Mamidwar

I am Shweta Mamidwar working as a Intern in Product Company. Likes to share knowledge.

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Share via
Copy link
Powered by Social Snap