What are Super Global Variables in PHP Example

What are Super Global Variables in PHP Example

There are so many predefined global variable in php. It can be accessible from any function, class or file without you having to create them first. Global variable were introduced from php4.1 which is always available in all scope.

Super Global variables are an array variable in PHP

List of PHP superglobal variables are given below:

  • $GLOBALS
  • $_SERVER
  • $_REQUEST
  • $_POST
  • $_GET
  • $_FILES
  • $_ENV
  • $_COOKIE
  • $_SESSION

PHP $GLOBALS (super global) variable

$GLOBAL is a php super global variable which contains all global variables in php script, including other superglobals.

PHP stores all global variable in an array. Example :

  1. <?php
  2. $x = 75;
  3. $y = 25;
  4. function substraction() {
  5. $GLOBALS['z'] = $GLOBALS['x'] - $GLOBALS['y'];
  6. }
  7. substraction();
  8. echo $z;
  9. ?>

Output of above code : 50

In the above example, since z is a variable present within the $GLOBALS array now it can be accessible from outside the function $_SERVER

$_SERVER is an array also known as PHP super global variable that holds information about headers, paths and script location and entries in these array are created by web servers.

Code Description
$_SERVER['PHP_SELF'] It is used to display the filename of the currently executing script
$_SERVER['GATEWAY_INTERFACE'] It is used to display the version of the Common Gateway Interface (CGI) the server is using
$_SERVER['SERVER_ADDR'] It is used to display the IP address of the host server
$_SERVER['SERVER_NAME'] It is used to display the host server's name 
$_SERVER['SERVER_SOFTWARE'] It is used to display the server identification string.
$_SERVER['SERVER_PROTOCOL'] It is used to display the name and revision of the information protocol
$_SERVER['REQUEST_METHOD'] It is used to display the request method used to access the page. i.e. 'GET','POST'
$_SERVER['REQUEST_TIME'] It is used to display the timestamp of the start of the request.
$_SERVER['QUERY_STRING'] It is used to display the query string if the page is accessed via a query string
$_SERVER['HTTP_ACCEPT'] It is used to display the Accept header from the current request
$_SERVER['HTTP_ACCEPT_CHARSET'] It is used to display the Accept_Charset header from the current request
$_SERVER['HTTP_HOST'] It is used to display the Host header from the current request
$_SERVER['HTTP_REFERER'] It is used to display the complete URL of the current page (not reliable because not all user-agents support it)
$_SERVER['HTTPS'] Is the script queried through a secure HTTP protocol
$_SERVER['REMOTE_PORT'] It is used to display the port being used on the user's machine to communicate with the web server
$_SERVER['SCRIPT_FILENAME'] It is used to display the absolute pathname of the currently executing script
$_SERVER['SERVER_ADMIN'] It is used to display the value given to the SERVER_ADMIN directive in the web server configuration file.
$_SERVER['REMOTE_ADDR'] It is used to display the IP address from where the user is viewing the current page
$_SERVER['REMOTE_HOST'] It is used to display the Host name from where the user is viewing the current page
$_SERVER['SERVER_PORT'] It is used to display the port which is being used on the local machine to communicate with the web server.
$_SERVER['SERVER_SIGNATURE'] It is used to display the server version and virtual host name which are added to server-generated pages
$_SERVER['PATH_TRANSLATED'] It is used to display the file system based path to the current script
$_SERVER['SCRIPT_NAME'] It is used to display the current script's path
$_SERVER['SCRIPT_URI'] It is used to display the current page's URI
$_REQUEST

$_REQUEST is used to access the data after submission of HTML Form. Form method can be either 'GET' or 'POST'.

Here is simple example where i have submit form which is point to itself for process with data with GET method. Now in PHP script i am using the super global variable $_REQUEST to get the value of the input field.

  1. <html>
  2. <body>
  3. <form action="<?php echo $_SERVER['PHP_SELF'];?>" method="get" >
  4. Product Name: <input type="text" name="product_name">
  5. <input type="submit">
  6. </form>
  7. <?php
  8. if ($_SERVER["REQUEST_METHOD"] == "GET") {
  9. if ($_REQUEST['product_name']) {
  10. echo $_REQUEST['product_name'];
  11. } else {
  12. echo "Product Name is empty";
  13. }
  14. }
  15. ?>
  16. </body>
  17. </html>
$_POST

$_POST super global variable is used to access form data if form is submitted with POST method.

$_GET

$_GET super global variable is used to access form data if form is submitted with GET method.

Here is simple URL with some query string now we will use GET method to access the url data.

http://www.expertphp.in/article?s=laravel
  1. <?php
  2. echo "Keyword which is used to search is : " . $_GET['s'];
  3. ?>

Phone: (+91) 8800417876
Noida, 201301
sakarya escort akyazı escort arifiye escort erenler escort eve gelen escort ferizli escort geyve escort hendek escort otele gelen escort sapanca escort söğütlü escort taraklı escort
sakarya escort akyazı escort arifiye escort erenler escort eve gelen escort ferizli escort geyve escort hendek escort karapürçek escort karasu escort kaynarca escort kocaali escort otele gelen escort pamukova escort sapanca escort söğütlü escort taraklı escort
sakarya escort akyazı escort arifiye escort erenler escort eve gelen escort ferizli escort geyve escort hendek escort karapürçek escort karasu escort kaynarca escort kocaali escort otele gelen escort pamukova escort sapanca escort söğütlü escort taraklı escort
sakarya escort akyazı escort arifiye escort erenler escort eve gelen escort ferizli escort geyve escort hendek escort karapürçek escort karasu escort kaynarca escort kocaali escort otele gelen escort pamukova escort sapanca escort söğütlü escort taraklı escort
sakarya escort serdivan escort adapazarı escort sakarya escort bayan sakarya escort sakarya escort sakarya escort sakarya escort sakarya escort sakarya escort sakarya escort sakarya escort sakarya escort sakarya escort sapanca escort sakarya escort