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
$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 :
- <?php
- $x = 75;
- $y = 25;
- function substraction() {
- $GLOBALS['z'] = $GLOBALS['x'] - $GLOBALS['y'];
- }
- substraction();
- echo $z;
- ?>
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
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.
- <html>
- <body>
- <form action="<?php echo $_SERVER['PHP_SELF'];?>" method="get" >
- Product Name: <input type="text" name="product_name">
- <input type="submit">
- </form>
- <?php
- if ($_SERVER["REQUEST_METHOD"] == "GET") {
- if ($_REQUEST['product_name']) {
- echo $_REQUEST['product_name'];
- } else {
- echo "Product Name is empty";
- }
- }
- ?>
- </body>
- </html>
$_POST
super global variable is used to access form data if form is submitted with POST
method.
$_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
- <?php
- echo "Keyword which is used to search is : " . $_GET['s'];
- ?>