We already have discussed about variables and global variables in PHP in the post PHP | Variables and Data Types. In this article, 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 detail:
- $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
<?php $x = 300; $y = 200; function multiplication(){ $GLOBALS [ 'z' ] = $GLOBALS [ 'x' ] * $GLOBALS [ 'y' ]; } multiplication(); echo $z ; ?> |
Output :
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:
PHP
<?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' ]; echo "<br>" ?> |
Output :
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:
HTML
<!DOCTYPE html> < html > < body > < form method = "post" action="<?php echo $_SERVER['PHP_SELF'];?>"> NAME: < input type = "text" name = "fname" > < button type = "submit" >SUBMIT</ button > </ form > <? php if ($_SERVER["REQUEST_METHOD"] == "POST") { $ name = htmlspecialchars ($_REQUEST['fname']); if(empty($name)){ echo "Name is empty"; } else { echo $name; } } ?> </ body > </ html > |
Output :
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:
HTML
<!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" > < button type = "submit" >SUBMIT</ button > </ form > <? php $nm=$_POST['name']; $age=$_POST['age']; echo "<strong>".$nm." is $age years old.</ strong >"; ?> </ body > </ html > |
Output :
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:
HTML
<!DOCTYPE html> < html > < head > < title ></ title > </ head > < body bgcolor = "cyan" > <? php $name = $_GET['name']; $city = $_GET['city']; echo "<h1>This is ".$name." of ".$city."</ h1 >< br >"; ?> < img src = "2.jpg" alt = "nanilake" height = "400" width = "500" /> </ body > </ html > |
We 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 parameters 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.