Posted By:
9 Mar, 2014 6:12 am PDT

What are different PHP Superglobal arrays and their use


What are different PHP Superglobal arrays and their use?

When information is passed from one web page to other web page or some other script, it has to be stored on some global variable which can be accessed regardless of scope of script.


There are different superglobals and their use.

a)     $GLOBALS

$GLOBALS superglobal array allow you to access global variable .All variables in the global scope are loaded into the $GLOBALS array, allow you to access them using the variable name as the array key.

Syntax:- $GLOBALS[‘variableName’]

b)                  $_SERVER

The $_SERVER superglobal stores information about the server and the current script. It also has features that allow you to access the IP address of a site visitor .One of the most useful pieces of information available in the $_SERVER superglobal is the name of the host site, which is stored in HTTP_HOST




c)                   $_GET

This array is used to collect data send from HTTP get method. One of the two most common methods of passing data between pages is the GET method. GET data is passed through a query string in the URL, which appears in browser address bar ,looks something like this:


Syntax:- $_GET[‘inputName’];


d)                  $_POST

This array is used to collect data send from HTTP post method. PHP provides a second method for sending data POST. Structurally, POST requests are very similar to GET requests, and you access their values in the same way. Unlike GET, data will not be appeared in the address bar of browser after submitting data.


Syntax:- $_POST[‘inputName’];

e)                  $_REQUEST

This array is used to collect all data send from HTTP form.The $_REQUEST superglobal is an array that contains the contents of the $_GET, $_POST, and $_COOKIE superglobals.

f)                   $_FILES

This array is used to collect data passed by an HTML file input. In an application such as the job portals, where you uploads CV and images. In order to access an uploaded

file, you need to use the $_FILES superglobal. $_FILES works a little differently from $_POST and $_GET in that each file creates an array of related elements that provide information about the uploaded file. The provided information



·                     name: The file name

·                     type: The file type

·                     tmp_name: The temporary location of the uploaded file

·                     error: An error code corresponding to error type (0 if no errors found)

·                     size: The file size in bytes


g)                  $_SESSION

This array is used to maintained current session data specific to the user, the $_SESSION superglobal provides a practical, easy solutionOne common use of $_SESSION variables is to store a user’s login status.




h)                  $_COOKIE

Cookies behave similarly to sessions, but they allow you to store information on a user’s machine for a longer period of time. Information stored in cookies remains available even after the user closes her browser.

In PHP, you use the $_COOKIE superglobal to access cookie values.

echo " <h2 > " ;
echo " To demonstrate SuperGlobals in PHP <br > < br > " ;
echo " Welcome to { $_SERVER [ ’ SERVER_NAME ’]} <br > < br > " ;
echo " You are currently using page is { $_SERVER [ ’ PHP_SELF ’]} <←
 br > < br > " ;
$name = " Clitus " ; // Global variable
$age = 20; // Global Variable
tempFunction () ;
function tempFunction ()
 $name = " Oliver " ;// local Variable
$age = 25;// local variable
echo " Local Variable Value <br > " ;
echo " Name : { $name } < br > " ;
echo " Age : { $age } " ;
echo " <br > < br > Global Variable Value <br > " ;
echo " Name : { $GLOBALS [ ’ name ’]} < br > " ;
// GLOBALS [ ’ ’] is used to display value of global variable
echo " Age : { $GLOBALS [ ’ age ’]} " ;
// GLOBALS [ ’ ’] is used to display value of global variable







Attachment: SuperGlobals.php

Please login to reply to this problem.

Related Problems on Fixoncloud