PHP Global Variables Example
In this example we will learn about PHP global variables, what they are and how to use them. Variables are an integral part of any programming language. They are used to hold information about the programming context.
Depending on where the variable is declared, it may be visible or invisible in other parts of the script(the scope of the variable can vary). Global Variables or superglobals are visible from any part of the PHP script(they are visible in all scopes)
For this example we will use:
- A computer with PHP>= 5.5 installed
- notepad++
1. Global Variables or superglobals
Superglobals were introduced in PHP 4.1.0. The superglobals are:
- $GLOBALS
- $_SERVER
- $_REQUEST
- $_POST
- $_GET
- $_FILES
- $_ENV
- $_COOKIE
- $_SESSION
1.1 $GLOBALS
The $GLOBALS
superglobal is an associative array which references all global variables in the script. The name of the variable is the key for $GLOBALS
associative array.
index.php
<?php $des="PHP is a great scripting language."; function check(){ $des="Java is a great programming language, heard java 9 will be out soon."; echo "$des<br>";// we print the local variable echo $GLOBALS["des"];// we print the global variable } check(); ?>
In line six and seven we print both local variable and global variable.
1.2 $_SERVER
The $_SERVER
holds server information. Information such as headers, paths and script location.
index2.php
<!DOCTYPE html> <html lang=en> <head> <style> html, body{ width:100%; height:100%; margin:0%; font-family:"helvetica","verdana","calibri", "san serif"; overflow:hidden; padding:0%; border:0%; } </style> <meta charset="utf-8" /> <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no, target-densitydpi=device-dpi"/> <title>PHP GLOBAL EXAMPLE</title> </head> <body> <?php echo $_SERVER [ 'PHP_SELF' ];// holds the filename about the current executing script echo "<br>" ; echo $_SERVER [ 'SERVER_ADDR' ];//holds the ip address of the server which the current script is executing echo "<br>" ; echo $_SERVER [ 'SERVER_NAME' ];// The name of the server host under which the current script is executing. echo "<br>" ; echo $_SERVER [ 'REQUEST_METHOD' ];// Which request method was used to access the page; i.e. ' GET', ' POST echo "<br>" ; echo $_SERVER [ 'REQUEST_TIME' ]; echo "<br>" ;// The timestamp of the start of the request. Available since PHP 5.1.0. echo $_SERVER [ 'HTTP_HOST' ];// Contents of the Host: header from the current request, if there is one. echo "<br>" ; if(isset($_SERVER [ 'HTTP_REFERER' ] )) echo $_SERVER [ 'HTTP_REFERER' ];// The address of the page (if any) which referred the user agent to the current page. This is set by the user agent. Not all user agents will set this echo "<br>" ; echo $_SERVER [ 'HTTP_USER_AGENT' ];// Contents of the User-Agent: header from the current request, if there is one. This is a string denoting the user agent being which is accessing the page. echo "<br>" ; echo $_SERVER [ 'SCRIPT_NAME' ];// Contains the current script's path. ?> </body> </html>
The comments in the code explains the meaning of each superglobal variable. The list below contains some elements that can go inside $_SERVER:
Note: You may or may not find any of the following elements in $_SERVER .
- SERVER_SOFTWARE: Server identification string, given in the headers when responding to requests.
- SERVER_PROTOCOL: Name and revision of the information protocol via which the page was requested. i.e. ‘ HTTP/1.0 ‘
- REQUEST_TIME: The timestamp of the start of the request. Available since PHP 5.1.0.
- REQUEST_TIME_FLOAT: The timestamp of the start of the request, with microsecond precision. Available since
PHP 5.4.0. - QUERY_STRING: The query string, if any, via which the page was accessed.
- HTTP_CONNECTION: Contents of the Connection: header from the current request, if there is one. Example: ‘ Keep-Alive ‘.
- HTTP_REFERER: The address of the page (if any) which referred the user agent to the current page. This is set by the user agent. Not all user agents will set this, and some provide the
ability to modify HTTP_REFERER as a
feature. In short, it cannot really be trusted. - SERVER_PORT: The port on the server machine being used by the web server for communication. For default setups, this will be ‘ 80 ‘; using SSL, for
instance, will change this to whatever your defined secure HTTP port is.
1.3 $_REQUEST
This superglobal variable holds the contents of$_GET
, $_POST
and $_COOKIE
index3.php
<!DOCTYPE html> <html lang=eng> <head> <style> html, body{ width:100%; height:100%; margin:0%; font-family:"helvetica","verdana","calibri", "san serif"; overflow:hidden; padding:0%; border:0%; } </style> <meta charset="utf-8" /> <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no, target-densitydpi=device-dpi"/> <title>HTML Form</title> </head> <body><?php if ($_SERVER['REQUEST_METHOD' ] == 'POST' ){ echo $_REQUEST['name']."<br>"; echo $_REQUEST['occu']; } else{ ?> <form action=<? echo $_SERVER [ 'PHP_SELF' ]; ?> method ="POST"> <label for=name>First name</label><br> <input type ="text" name ="name" id=name required> <br><br> <label for=occu>Occupation</label><br> <input type ="text" name ="occu" required><br><br> <input type="submit" value=submit> </form> <?php } ?> </body> </html>
1.4 $_POST
The $_POST superglobal associative array is used to access all the information sent using the POST method.
index4.php
<!DOCTYPE html> <html lang=eng> <head> <style> html, body{ width:100%; height:100%; margin:0%; font-family:"helvetica","verdana","calibri", "san serif"; overflow:hidden; padding:0%; border:0%; } </style> <meta charset="utf-8" /> <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no, target-densitydpi=device-dpi"/> <title>HTML Form</title> </head> <body><?php if ($_SERVER['REQUEST_METHOD' ] == 'POST' ){ echo $_POST['name']."<br>"; echo $_POST['occu']; } else{ ?> <form action=<? echo $_SERVER [ 'PHP_SELF' ]; ?> method ="POST"> <label for=name>First name</label><br> <input type ="text" name ="name" id=name required> <br><br> <label for=occu>Occupation</label><br> <input type ="text" name ="occu" required><br><br> <input type="submit" value=submit> </form> <?php } ?> </body> </html>
1.5 $_GET
The $_GET superglobal associative array is used to access all the information sent using the GET method.
index5.php
<!DOCTYPE html> <html lang=eng> <head> <style> html, body{ width:100%; height:100%; margin:0%; font-family:"helvetica","verdana","calibri", "san serif"; overflow:hidden; padding:0%; border:0%; } </style> <meta charset="utf-8" /> <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no, target-densitydpi=device-dpi"/> <title>HTML Form</title> </head> <body> <?php echo $_GET [ "name" ]; ?> </body> </html>
Assuming the user entered http://example.com/?name=john. The above example will output john
1.6 $_FILES
This superglobal variable holds items supplied to the script through HTTP POST.
1.7 $_ENV
This superglobal variable is an associative array of variables passed to the current script via the environment method.
1.8 $_COOKIE
This is a super global variable which is used to manipulate cookie data.
index6.php
<!DOCTYPE html> <html lang=eng> <head> <style> html, body{ width:100%; height:100%; margin:0%; font-family:"helvetica","verdana","calibri", "san serif"; overflow:hidden; padding:0%; border:0%; } </style> <meta charset="utf-8" /> <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no, target-densitydpi=device-dpi"/> <title>superglobal example</title> </head> <body> <?php if(isset($_COOKIE["name"])) echo $_COOKIE[ "name" ]; else echo "This cookie is not set"; ?> </body> </html>
This checks if $_COOKIE["name"]
is set, if it is set print out it’s value else print out an error message.
1.8 $_SESSION
$_SESSION
is a superglobal which holds session variable.
index7.php
<?php session_start (); /* this signifies session has started if you don't write this line, you can't can't use $_Session global variable*/ $_SESSION ["name"]="john"; //prints out the session variable function post(){ echo $_SESSION["name"]."<br>"; } //update SESSION variable function change($d){ $_SESSION [ "name" ]= $d; } ?> <!DOCTYPE html> <html lang=eng> <head> <style> html, body{ width:100%; height:100%; margin:0%; font-family:"helvetica","verdana","calibri", "san serif"; overflow:hidden; padding:0%; border:0%; } </style> <meta charset="utf-8" /> <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no, target-densitydpi=device-dpi"/> <title>superglobal example</title> </head> <body> <?php post();//print out session variable change("mark");// update session variable post();//print out session variable ?> </body> </html>
2. Summary
In this example we learnt about PHP global variables. We learnt about $GLOBALS
, $_SERVER
, $_REQUEST
, $_POST
, $_GET
, $_FILES
, $_ENV
, $_COOKIE and
$_SESSION