Updated March 27, 2023
Introduction to PHP Superglobal Variables
In PHP, some of the variables are predefined in the package itself for them to be used in inappropriate places. These predefined variables play a major role in webpage development in PHP. These variables are called “superglobal” variables. The term globalization refers to the context that it can be accessed globally irrespective of the scope you can access it anywhere in the program. These variables are used in web page development in PHP and these variables have a predefined functionality and can be used whenever required.
List of PHP Superglobal Variables
The superglobal variables in PHP can be accessed anywhere and anytime whenever required for its functionality.
Some of the superglobal variables in PHP are as follows:
1. $GLOBALS
PHP $GLOBAL variable is used to use the value of the variable globally and can be used anywhere in the program. PHP stores the global variables in an array called $GLOBALS [index]. The index here plays an important role as it stores the name of the variables. So whenever we use the variable with the global keyword automatically the value will be set and operation will be performed.
Code:
<?php
$x = 75;
$y = 25;
$z = 100;
function addup()
{
$GLOBALS['a'] = $GLOBALS['x'] + $GLOBALS['y'] + $GLOBALS['z'];
}
addup();
echo $a;
?>
Output:
2. $_SERVER
This superglobal variable is used to capture the information about headers, path locations, and scripts location. This variable is used in most of the places where the address and path locations have to be printed.
Some of them are as follows:
- $_SERVER[‘PHP_SELF’]: This variable is used to print or return the current filename that is getting executed.
- $_SERVER[‘SERVER_ADDR’]: This variable is used to return the Internet Protocol(IP) address of the host server.
- $_SERVER[‘REQUEST_METHOD’]: This variable is used to return the method that has been used in the program for submission i.e. either GET or POST.
- $_SERVER[‘SCRIPT_NAME’]: This variable returns the path of the current script that the user is running for execution.
Code:
<?php
echo $_SERVER['PHP_SELF'];
echo "<br>";
echo $_SERVER['SERVER_ADDR'];
echo "<br>";
?>
Output:
3. $_REQUEST
This super global variable is used in the code to return the data i.e. collection of data when a form is submitted. This variable is used when forms are used in the program. When the user fills the data in the form it is then submitted using any one method i.e. GET or POST. When the user submits the form using the GET method the content is visible and the data can be seen by the user. When the POST method is used the data is hidden and invisible to the user. The data can be collected with this super global variable $_REQUEST.
Code:
<!DOCTYPE html>
<html>
<body>
<form method="POST" action="<?php echo $_SERVER['PHP_SELF'];?>">
Ename: <input type="text" name="ename">
ID: <input type="text" name="eid">
<input type="submit">
</form>
<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
// collect value of input field
$name = htmlspecialchars($_REQUEST['ename']);
$id= htmlspecialchars($_REQUEST['eid']);
if (empty($name)) {
echo "Employee Name is empty";
}
else
{
echo $name;
}
if (empty($id)) {
echo "Employee ID is empty";
} else {
echo $id;
}
}
?>
</body>
</html>
Output:
4. $_POST
This super global variable is used when the user has to submit the form and in the form action, we need to specify which type the form has to be submitted. This method collects the data after the user submits the form and the method specified in the form action will be $_POST. This method hides the data that has been entered by the user. This method is mostly used for login purposes, encryption, etc.
Code:
<!DOCTYPE html>
<html>
<body>
<form method="POST" action="<?php echo $_SERVER['PHP_SELF'];?>">
Ename: <input type="text" name="ename">
<input type="submit">
</form>
<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$name = htmlspecialchars($_REQUEST['ename']);
if (empty($name)) {
echo "Employee Name is empty";
}
else
{
echo $name;
}
}
?>
</body>
</html>
Output:
5. $_GET
This super global variable is generally used to display the content or data entered by the user. This variable helps the data to be collected and makes it visible to the user. The user can see the data in the URL of the page. This variable is not secure and should not be used for security reasons. This variable is generally specified in the form action method where the action has to be performed when the user submits the form.
Code:
<!DOCTYPE html>
<html>
<body>
<a href="demo_get.php?subject=PHP&web=google.com">Example for $GET</a>
</body>
</html>
Output:
Importance of Super Global Variable in PHP
The main objective of using super global variables in PHP is that the developer can easily use the built-in functions of PHP that comes with the package in the program. These variables are built with a meaning that has to be used properly in the program. These variables can be accessed anywhere whenever required. These variables collect the data or content entered by the user and display it wherever required. So, if a user wishes to see the information that he has entered in the form, the developer can use this variable in the program to collect the information that has been entered by the user and display it on the screen.
There are few more super global variables in PHP like $_COOKIE, $_SESSION, $_ ENV, etc. This variable also has its scope to be used in the program. Cookies are used by the developer to save the data in that session so that when the user logs in next time, the data is provided to him. Sessions are used in PHP to create a timeframe for a particular page to get timed out once the session gets expired. This is generally used for security reasons and is mostly used in banking, social media, etc.
Conclusion
In this article, we discussed what is a superglobal variable and its scope in the script. Also, we discussed different types of superglobal variables and their functionality. The main purpose of using these variables is that it can be globally used by the developer.
Recommended Articles
This is a guide to PHP Superglobal Variables. Here we discuss the basic concept, list of PHP Superglobal Variables and the importance along with the examples. You can also go through our other related articles to learn more –