PHP Global Variables
PHP Global Variables
Several predefined variables in PHP are "superglobals", which means that they are always
accessible, regardless of scope - and you can access them from any function, class or file without
having to do anything special.
The PHP superglobal variables are:
$GLOBALS
$_SERVER
$_REQUEST
$_POST
$_GET
$_FILES
$_ENV
$_COOKIE
$_SESSION
This chapter will explain some of the superglobals, and the rest will be explained in later chapters.
PHP $GLOBALS
$GLOBALS is a PHP super global variable which is used to access global variables from anywhere
in the PHP script (also from within functions or methods).
PHP stores all global variables in an array called $GLOBALS[index]. The index holds the name of
the variable.
The example below shows how to use the super global variable $GLOBALS:
Example
<?php
$x = 75;
$y = 25;
function addition() {
$GLOBALS['z'] = $GLOBALS['x'] + $GLOBALS['y'];
}
addition();
echo $z;
?>
In the example above, since z is a variable present within the $GLOBALS array, it is also
accessible from outside the function!
PHP $_SERVER
$_SERVER is a PHP super global variable which holds information about headers, paths, and
script locations.
The example below shows how to use some of the elements in $_SERVER:
Example
<?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'];
?>
The following table lists the most important elements that can go inside $_SERVER:
Element/Code
$_SERVER['PHP_SELF']
Description
Returns the filename of the currently executing script
Returns the version of the Common Gateway Interface
$_SERVER['GATEWAY_INTERFACE']
(CGI) the server is using
$_SERVER['SERVER_ADDR']
Returns the IP address of the host server
Returns the name of the host server (such as
$_SERVER['SERVER_NAME']
www.w3schools.com)
Returns the server identification string (such as
$_SERVER['SERVER_SOFTWARE']
Apache/2.2.24)
Returns the name and revision of the information
$_SERVER['SERVER_PROTOCOL']
protocol (such as HTTP/1.1)
Returns the request method used to access the page
$_SERVER['REQUEST_METHOD']
(such as POST)
Returns the timestamp of the start of the request (such
$_SERVER['REQUEST_TIME']
as 1377687496)
Returns the query string if the page is accessed via a
$_SERVER['QUERY_STRING']
query string
$_SERVER['HTTP_ACCEPT']
Returns the Accept header from the current request
Returns the Accept_Charset header from the current
$_SERVER['HTTP_ACCEPT_CHARSET']
request (such as utf-8,ISO-8859-1)
$_SERVER['HTTP_HOST']
Returns the Host header from the current request
$_SERVER['HTTP_REFERER']
Returns the complete URL of the current page (not
2
$_SERVER['HTTPS']
$_SERVER['REMOTE_ADDR']
$_SERVER['REMOTE_HOST']
$_SERVER['SERVER_ADMIN']
$_SERVER['SERVER_PORT']
$_SERVER['SCRIPT_NAME']
$_SERVER['SCRIPT_URI']
PHP $_REQUEST
PHP $_REQUEST is used to collect data after submitting an HTML form.
The example below shows a form with an input field and a submit button. When a user submits the
data by clicking on "Submit", the form data is sent to the file specified in the action attribute of the
<form> tag. In this example, we point to this file itself for processing form data. If you wish to use
another PHP file to process form data, replace that with the filename of your choice. Then, we can
use the super global variable $_REQUEST to collect the value of the input field:
Example
<html>
<body>
<form method="post" action="<?php echo $_SERVER['PHP_SELF'];?>">
Name: <input type="text" name="fname">
<input type="submit">
</form>
<?php
$name = $_REQUEST['fname'];
echo $name;
?>
</body>
</html>
//string.php
<HTML>
<HEAD>
<BODY>
<?php
$st1=$_POST['t1'];
$st2=$_POST['t2'];
echo "1st String is: ".$st1."<br>";
echo "2nd String is: ".$st2."<br>"";
echo "Length of String is: ".strlen($st1)."<br>";
echo "Reverse of String is: ".strrev($st1)."<br>";
echo "Repeat String is: ".str_repeat($st1,3)."<br>";
echo "Substring String is: ".substr($st1,2,4)."<br>";
echo "Compare String is: ".strcmp($st1,$st2)."<br>";
if(strcmp($st1,$st2)==0)
{
echo "equal string...."."<br>";
}
else if(strcmp($st1,$st2)>1)
{
4
<HTML>
<HEAD>
</HEAD>
<BODY>
<?php
$file=fopen("demo.txt","r+");
while(!feof($file))
{
$data=fgets($file);
if($data=="\n")
echo"<br>";
echo $data."<br>";
}
$str="The Data is Written to file... Thank You";
fwrite($file, $str."<br>");
fclose($file);
?>
</BODY>
</HTML>