0% found this document useful (0 votes)
262 views6 pages

PHP Global Variables

Several predefined PHP variables called superglobals are always accessible regardless of scope. The main PHP superglobals are $GLOBALS, $_SERVER, $_REQUEST, $_POST, $_GET, $_FILES, $_ENV, $_COOKIE, and $_SESSION. $GLOBALS is a superglobal variable used to access global variables from anywhere in a PHP script, including within functions. $_SERVER is a superglobal that holds information about headers, paths, and locations. $_REQUEST is used to collect form data submitted to a PHP file.

Uploaded by

sumitbide
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
Download as docx, pdf, or txt
0% found this document useful (0 votes)
262 views6 pages

PHP Global Variables

Several predefined PHP variables called superglobals are always accessible regardless of scope. The main PHP superglobals are $GLOBALS, $_SERVER, $_REQUEST, $_POST, $_GET, $_FILES, $_ENV, $_COOKIE, and $_SESSION. $GLOBALS is a superglobal variable used to access global variables from anywhere in a PHP script, including within functions. $_SERVER is a superglobal that holds information about headers, paths, and locations. $_REQUEST is used to collect form data submitted to a PHP file.

Uploaded by

sumitbide
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1/ 6

PHP Global Variables - Superglobals

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']

reliable because not all user-agents support it)


Is the script queried through a secure HTTP protocol
Returns the IP address from where the user is viewing
the current page
Returns the Host name from where the user is viewing
the current page
Returns the value given to the SERVER_ADMIN
directive in the web server configuration file (if your
script runs on a virtual host, it will be the value
defined for that virtual host) (such as
someone@w3schools.com)
Returns the port on the server machine being used by
the web server for communication (such as 80)
Returns the path of the current script
Returns the URI of the current page

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>

// PHP script to demonstrate the different string functions.


<HTML>
<HEAD>
</HEAD>
<BODY>
<FORM METHOD="POST" ACTION="string.php">
<TABLE>
<TR> <TD>Enter String1 :</TD>
<TD><INPUT TYPE="text" NAME="t1"></TD>
</TR>
<TR> <TD>Enter String2 :</TD>
<TD><INPUT TYPE="text" NAME="t2"></TD>
</TR>
<TR>
<TD><INPUT TYPE="submit" VALUE="Click" name="b1"></TD>
</TR>
</TABLE>
</FORM>
</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

echo "1st string gretter than 2nd string...."."<br>";


}
else if(strcmp($st1,$st2)<0)
{
echo "2nd string gretter than 1st string...."."<br>";
}
echo "Count of words in String is: ".str_word_count($st1)."<br>";
echo "After Trim String is: ".trim($st1)."<br>";
echo "First letter is Capital is(ucfirst): ".ucfirst($st1)."<br>";
echo "All letters in capital is(ucwords): ".ucwords($st1)."<br>";
echo "After Replaceing String is: ".str_replace($st1,"to",2)."<br>";
?>
</BODY>
</HTML>

//PHP Script For Demonstrate File Handling (Read & Write).


===============================================================

<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>

You might also like