PHP Tutorial

What are PHP super global variables and their usage in PHP

superglobal-variable-in-php

PHP supports 8 types of predefined superglobal variables that are easily available in all scopes throughout a project. There is no need to define them global $variable; to use them within the script anywhere. Their list is below.

Below is the list of PHP Superglobal Variables’:

  1. $_GET
  2. $_POST
  3. $_REQUEST
  4. $_COOKIE
  5. $_FILES
  6. $_SESSION
  7. $_SERVER
  8. $GLOBALS

(1) $_GET Superglobal Variable:

The $_GET is an associative array. This superglobal variable helps us to receive the data of the HTML form submitted through the GET method and to receive the data sent by URL using a query string. An example is below:

Here we are assuming that the data is sent using URL http://www.phpglossary.com/?name = ‘PHP Glossary’

then we will get this value like this:

(2) $_POST Superglobal Variable:

The $_POST is an associative array. This superglobal variable is used to receive the data of the HTML form submitted through the POST method. An example is below:

suppose that the data is sent using a form submitted using the post method.

and we will get this value like this:

(3) $_REQUEST Superglobal Variable:

The $_REQUEST is an associative array. This helps us to get the HTML form data that is submitted, either using the GET method or POST method. In both cases we can easily use this superglobal variable. Besides getting form data this is also used to get the cookies data, which is already set in cookies.

(4) $_COOKIE Superglobal Variable:

The $_COOKIE is an associative array. This helps us to get the value of already set in a cookie. The cookie values are stored in the client’s browser and these stored values are again sent to the website when the same browser opens the same website again. Example of getting cookie value:

Here we are assuming that the ‘name’ cookie is already set before.

(5) $_FILES Superglobal Variable:

The $_FILES is an associative array. Whenever we upload a file using the HTML form with the POST method then we get this uploaded file using the $_FILES superglobal variable.  This superglobal variable contains all the information about the uploaded file like file name, file size, file type, error, and tmp_name. An example is below:

Here we are assuming that the HTML form is submitted by the post method and the input type file name was profile_photo.

So based on the above example we can say that $_FILES is a multi-dimensional associative array.

(6) $_SESSION Superglobal Variable:

The $_SESSION is an associative array. It is used to store information & access the same information in all pages of the project. Its information is stored on the server and this information automatically deleted when the browser is closed. An example is below:

(7) $_SERVER Superglobal Variable:

The $_SERVER is an associative array. This keeps the information about the script, path, headers, etc. The values in this associative array are store by the web server itself. We just need to access the information by passing the key name like as if we want to access the absolute path of the current script file then we need to pass ‘SCRIPT_FILENAME’ key in $_SERVER array like this

like as above example we can use following keys in $_SERVER superglobal variable to access the required information from the server.

  1. PHP_SELF
  2. argv
  3. argc
  4. GATEWAY_INTERFACE
  5. SERVER_ADDR
  6. SERVER_NAME
  7. SERVER_PROTOCOL
  8. SERVER_SOFTWARE
  9. REQUEST_TIME
  10. REQUEST_TIME_FLOAT
  11. QUERY_STRING
  12. DOCUMENT_ROOT
  13. HTTP_ACCEPT
  14. HTTP_ACCEPT_CHARSET
  15. HTTP_ACCEPT_ENCODING
  16. HTTP_CONNECTION
  17. HTTP_HOST
  18. HTTP_ACCEPT_LANGUAGE
  19. HTTP_REFERER
  20. HTTP_USER_AGENT
  21. HTTPS
  22. REQUEST_METHOD
  23. REMOTE_ADDR
  24. REMOTE_HOST
  25. REMOTE_USER
  26. REMOTE_PORT
  27. REDIRECT_REMOTE_USER
  28. SCRIPT_FILENAME
  29. SERVER_ADMIN
  30. SERVER_PORT
  31. SERVER_SIGNATURE
  32. PATH_TRANSLATED
  33. SCRIPT_NAME
  34. REQUEST_URI
  35. PHP_AUTH_DIGEST
  36. PHP_AUTH_USER
  37. PHP_AUTH_PW
  38. AUTH_TYPE
  39. PATH_INFO
  40. ORIG_PATH_INFO

(8) $GLOBALS Superglobal Variable:

The $GLOBALS helps us to access any variable globally in our project whose access scope is global. We can easily access that variable by just providing variable in as a key in $GLOBALS. An example is below

And below this the output:

PHP $globals superglobal variable
PHP $globals superglobal variable example output
Please rate this post
Sending
User Review
85% (1 vote)

About the author

Dinesh Sharma

I have worked on many CMS & Frameworks of PHP & I love to share my knowledge of what I am learning every day.

Add Comment

Click here to post a comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.