PHP Superglobal Variables

Spread the love

 

PHP Superglobal Variables

 

In PHP web application all the data which includes HTTP request are automatically handled by certain variables. These are PHP Superglobal Variables. These variables collect the information automatically when any HTTP request occurs.

The list of widely used PHP superglobal variables arrays are:

 

1. $_GET:

$_GET collects all the variable passed in the query string with URL.

For example, http://codingkala.com/tutorials.php?topic=variable&subtopic=superglobal,
here $_GET[‘topic’] will set “variable” and $_GET[‘subtopic’] will set to “superglobal“.

2. $_POST:

$_POST collects all the data send via post request. Post request generally sent through forms. This array will collect all the form field data submitted using form. Here the name of form fields becomes keys and form field values are assigned to respective array variables.

3. $_COOKIE:
$_COOKIE will collect all the cookies information from the browser. The key of this variable will be the name of cookies and cookies values will be the value of that cookies array variable.

$_SESSION:
While running any PHP script it comes under its own session. We can save that script session information and access using $_SESSION associative array variable.

4. $_SERVER:
In $_SERVER all the web server related variables get set. Like remote address(REMOTE_ADDR), server name, etc. We can use it as $_SERVER[‘REMOTE_ADDR’], etc. This array also contains information about the currently executing script’s location, header information, the path of script etc.

5. $_ENV:
When we start our web server, few of environment related variables gets set in $_ENV.

 

6. $_REQUEST:
In $_REQUEST all the variables which are set in $_GET, $_POST and $_COOKIE, gets set. We can use $_REQUEST in place of $_GET, $_POST and $_COOKIE also.

7. $_FILES:
$_FILES contains all the information of files uploaded using forms. It contains five variable types as keys, those are ‘name’, ‘type’,’tmp_name’, ‘error’ and ‘size’.

Example: In form, file input can be written as

<input type="file" name="photo" />

When we submit this form, $_FILES will contain below information:
$_FILES[‘photo’][‘name’] =>photo.jpg
$_FILES[‘photo’][‘type’] =>image/jpg
$_FILES[‘photo’][‘tmp_name’] =>/tmp/shdfer34
$_FILES[‘photo’][‘error’] =>0
$_FILES[‘photo’][‘size’] =>123212If $_FILES[‘photo’][‘error’] is equlas to zero then it means there is no error while uploading file. Other than zero indicates specific error.

8. $GLOBALS:
$GLOBALS array contains a reference to all the global variables within the currently executing script. We can access the global variable through $GLOBALS using variable name as a key in $GLOBALS.

9. PHP_SELF:
This variable cotains the name of current executing script. We use it inside $_SERVER as a key, like $_SERVER[‘PHP_SELF’];

10. $php_errormsg:
We can get last generated error message using $php_errormsg.


Spread the love

Leave a Comment