PHP Super Global Variables


Some predefined variables in PHP are "superglobals" ,which means they are always available, regardless of scope, and may be accessed from any function, class, or file without any particular effort.

PHP superglobals in PHP variables :

  • $GLOBALS
  • $_SERVER
  • $_REQUEST
  • $_POST
  • $_GET
  • $_FILES
  • $_ENV
  • $_COOKIE
  • $_SESSION
You can also search for these topics, global variables in php, types of superglobals in php, php superglobal variables, php global variable declaration, php global variable example, php global variable function.

PHP $GLOBALS Variable

$GLOBALS is a super global PHP variable used to access global variables in PHP scripts from any place (also from within functions or methods).

PHP keeps track of all global variables in an array named $GLOBALS[index]. The variable name is included in the index.

Example :- The following example demonstrates how to utilize a super global variable.

<?php
$x = 75;
$y = 25;
 
function addition() {
  $GLOBALS['z'] = $GLOBALS['x'] + $GLOBALS['y'];
}
 
addition();
echo $z;
?>

Output :-

100

Since $z is a variable within the $GLOBALS array, it is also available from outside the function in the example above!

You can also search for these topics, $globals in php, php $globals example, php globals in function, php globals undefined index, php $globals good practice, variables super globals php $GLOBALS.

PHP $_SERVER Variable

Super global variables are built-in variables, which always may be found in every scope.

$_SERVER is an overall PHP variable that contains Headers, Paths, and Locations of the script information.

Example :- The following example demonstrates how to take advantage of some of the elements in $_SERVER :

<?php
echo $_SERVER['PHP_SELF'];
echo "<br />";
echo $_SERVER['SERVER_NAME'];
echo "<br />";
echo $_SERVER['HTTP_HOST'];
echo "<br />";
echo $_SERVER['HTTP_REFERER'];
echo "<br />";
echo $_SERVER['HTTP_USER_AGENT'];
echo "<br />";
echo $_SERVER['SCRIPT_NAME'];
?>

Output :-

/demo/demo_global_server.php
35.194.26.41
35.194.26.41
https://tryphp.w3schools.com/showphp.php?filename=demo_global_server
Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.77 Safari/537.36
/demo/demo_global_server.php

The following table lists the most important elements that can go inside $_SERVER :

Element/Code Description
$_SERVER['PHP_SELF'] The current script's filename is returned.
$_SERVER['GATEWAY_INTERFACE'] server is The Common Gateway Interface (CGI) version the user is using is returned.
$_SERVER['SERVER_ADDR'] The host server's IP address is returned.
$_SERVER['SERVER_NAME'] The host server's name is returned (such as www.simmanchith.com)
$_SERVER['SERVER_SOFTWARE'] The server identification string (e.g., Apache/2.2.24) is returned.
$_SERVER['SERVER_PROTOCOL'] The information protocol's name and revision (for example, HTTP/1.1) are returned.
$_SERVER['REQUEST_METHOD'] The page's request method is returned (such as POST).
$_SERVER['REQUEST_TIME'] The timestamp of the request's start is returned (such as 1377687496).
$_SERVER['QUERY_STRING'] If the page was reached through a query string, the query string is returned.
$_SERVER['HTTP_ACCEPT'] The Accept header from the current request is returned.
$_SERVER['HTTP_ACCEPT_CHARSET'] The Accept Charset header from the current request (for example, utf-8, ISO-8859-1) is returned.
$_SERVER['HTTP_HOST'] The current request's Host header is returned.
$_SERVER['HTTP_REFERER'] Returns the whole URL (not trustworthy, since not all users support it) of the current page.
$_SERVER['HTTPS'] Is the script accessed using an encrypted HTTP protocol?
$_SERVER['REMOTE_ADDR'] The IP address of the user who is now browsing the page is returned.
$_SERVER['REMOTE_HOST'] The name of the host from which the user is seeing the current page is returned.
$_SERVER['REMOTE_PORT'] The port used to communicate with the web server on the user's system is returned.
$_SERVER['SCRIPT_FILENAME'] The absolute pathname of the currently running script is returned.
$_SERVER['SERVER_ADMIN'] Returns the value specified in a web site configuration file to the SERVER ADMIN directive (if you have your script running on a virtual Host, it will be a virtual host value) (such as somebody@w3schools.com)
$_SERVER['SERVER_SIGNATURE'] The server version and virtual host name that appear on server-generated pages are returned.
$_SERVER['PATH_TRANSLATED'] The current script's file system-based path is returned.
$_SERVER['SCRIPT_NAME'] The current script's path is returned.
$_SERVER['SCRIPT_URI'] The current page's URI is returned.
You can also search for these topics, $server in php, php $server example, php $server in function, php globals undefined index, php $globals good practice, variables super globals php $GLOBALS, php global $server variable, php server list, php sever path, php element/code server in server variable.

PHP $_GET Variable

The $_GET is a global variable that collects form data with method="get" after the form submission.

$_GET can also collect data sent in the URL.

Assume we have an HTML page that contains a hyperlink with parameters :

<html>
<body>

<a href="demo.php?name=John&web=simmanchith.com">Test $GET</a>

</body>
</html>

When a user hits the link "Test $GET", the parameters "name" and "web" are passed to "demo.php", and their values may then be accessed using $_GET in "demo.php." :

Example :- The example below shows the code in "test_get.php" :

<html>
<body>

<?php
echo "Name " . $_GET['name'] . " at " . $_GET['web'];
?>

</body>
</html>

Output :-


You can also search for these topics, which superglobal have get method, $get variable in php, php $get example, php $get good practice, variables super globals php $get, php variables get method.

PHP $_POST Variable

PHP $_POST is a global variable that collects data once an HTML form is submitted.

PHP$_POST is a PHP super global variable used after submission of the method="post" in the form of data collection. Uses $_POST to get variables are extensively utilized.

You can also search for these topics, which superglobal have post method, $post variable in php, php $post example, php $post good practice, variables super globals php $post, php variables post method, php post to url, php post request.

PHP $_REQUEST Variable

Super global variables are built-in variables, which always may be found in every scope.

PHP $_REQUEST is a global variable that collects data once an HTML form is submitted.

You can search for these topics, $request in php, php $request example, php $request good practice, variables super globals php $request, php request url, php request method, php request variable.