We already have discussed about variables and global variables in PHP in the post PHP | Variables and Data Types. In this artile we will learn about superglobals in PHP.
These are specially-defined array variables in PHP that make it easy for you to get information about a request or its context. The superglobals are available throughout your script. These variables can be accessed from any function, class or any file without doing any special task such as declaring any global variable etc. They are mainly used to store and get information from one page to another etc in an application.
Below is the list of superglobal variables available in PHP:
- $GLOBALS
- $_SERVER
- $_REQUEST
- $_GET
- $_POST
- $_SESSION
- $_COOKIE
- $_FILES
- $_ENV
Let us now learn about some of these superglobals in details:
- $GLOBALS : It is a superglobal variable which is used to access global variables from anywhere in the PHP script. PHP stores all the global variables in array $GLOBALS[] where index holds the global variable name, which can be accessed.
Below program illustrates the use of $GLOBALS in PHP:
<?php$x= 300;$y= 200;functionmultiplication(){$GLOBALS['z'] =$GLOBALS['x'] *$GLOBALS['y'];}multiplication();echo$z;?>chevron_rightfilter_noneOutput :
60000
In the above code two global variables are declared $x and $y which are assigned some value to them. Then a function multiplication() is defined to multiply the values of $x and $y and store in another variable $z defined in the GLOBAL array.
-
$_SERVER : It is a PHP super global variable that stores the information about headers, paths and script locations. Some of these elements are used to get the information from the superglobal variable $_SERVER.
Below program illustrates the use of $_SERVER in PHP:
<?phpecho$_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'];echo"<br>"?>chevron_rightfilter_noneOutput :

In the above code we used the $_SERVER elements to get some information. We get the current file name which is worked on using ‘PHP_SELF’ element. Then we get server name used currently using ‘SERVER_NAME’ element. And then we get the host name through ‘HTTP_HOST’.
-
$_REQUEST : It is a superglobal variable which is used to collect the data after submitting a HTML form. $_REQUEST is not used mostly, because $_POST and $_GET perform the same task and are widely used.
Below is the HTML and PHP code to explain how $_REQUEST works:
<!DOCTYPE html><html><body><formmethod="post"action="<?php echo $_SERVER['PHP_SELF'];?>">NAME: <inputtype="text"name="fname"><buttontype="submit">SUBMIT</button></form><?phpif ($_SERVER["REQUEST_METHOD"] == "POST") {$name=htmlspecialchars($_REQUEST['fname']);if(empty($name)){echo "Name is empty";} else {echo $name;}}?></body></html>chevron_rightfilter_noneOutput :

In the above code we have created a form that takes the name as input from the user and prints it’s name on clicking of submit button. We transport the data accepted in the form to the same page using $_SERVER[‘PHP_SELF’] element as specified in the action attribute, because we manipulate the data in the same page using the PHP code. The data is retrieved using the $_REQUEST superglobal array variable
-
$_POST : It is a super global variable used to collect data from the HTML form after submitting it. When form uses method post to transfer data, the data is not visible in the query string, because of which security levels are maintained in this method.
Below is the HTML and PHP code to explain how $_POST works:
<!DOCTYPE html><html><body><formmethod="post"action="<?php echo $_SERVER['PHP_SELF'];?>"><labelfor="name">Please enter your name: </label><inputname="name"type="text"><br><labelfor="age">Please enter your age: </label><inputname="age"type="text"><br><inputtype="submit"value="Submit"><buttontype="submit">SUBMIT</button></form><?php$nm=$_POST['name'];$age=$_POST['age'];echo "<strong>".$nm." is $age years old.</strong>";?></body></html>chevron_rightfilter_noneOutput :

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.
-
$_GET : $_GET is a super global variable used to collect data from the HTML form after submitting it. 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.
Below is the HTML and PHP code to explain how $_GET works:
<!DOCTYPE html><html><head><title></title></head><bodybgcolor="cyan"><?php$name = $_GET['name'];$city = $_GET['city'];echo "<h1>This is ".$name." of ".$city."</h1><br>";?><imgsrc="2.jpg"alt="nanilake"height="400"width="500"/></body></html>chevron_rightfilter_noneWe are actually seeing half of the logic just now. In the above code we have created a hyperlink image of Nainital Lake which will take us to picture.php page and with it will also take the paramerters name=”Nainilake” and city=”Nainital”.
That is when we click on the small image of Nainital Lake we will be taken to the next page picture.php along with the parameters. As the default method is get, these parameters will be passed to the next page using get method and they will be visible in the address bar. When we want to pass values to an address they are attached to the address using a question mark (?).
Here the parameter name=Nainilake is attached to the address. If we want to add more values, we can add them using ampersand (&) after every key-value pair similarly as city=Nainital is added using ampersand after the name parameter. Now after clicking on the image of Nainital Lake we want the picture.php page to be displayed with the value of parameter displayed along with it.
Recommended Posts:
- PHP 5 vs PHP 7
- PHP | Get PHP configuration information using phpinfo()
- PHP | php.ini File Configuration
- How to import config.php file in a PHP script ?
- LAMP installation and important PHP configurations on Ubuntu
- PHP | imagecreatetruecolor() Function
- Maximum execution time taken by a PHP Script
- PHP program to fetch data from localhost server database using XAMPP
- PHP | ImagickDraw getTextAlignment() Function
- PHP | Ds\Sequence last() Function
- PHP | Imagick floodFillPaintImage() Function
- Function to escape regex patterns before applied in PHP
- PHP | array_udiff_uassoc() Function
- PHP | Different characters in the given string
- PHP | geoip_continent_code_by_name() Function
- How to secure database passwords in PHP?
- PHP | GmagickPixel setcolor() function
- PHP | Calendar Functions Complete Reference
- PHP | opendir() Function
- PHP | cal_to_jd() Function
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.
Improved By : AbhinayTiwari


