PHP Predefined Variables


Introduction

Any PHP script has access to a number of predefined variables. However, many of them are dependent on web server software, PHP version and other factors. Some of these variables are not available for a script running in command line mode. The $GLOBALS variable stores references of all globally available predefined varibles. Most of these variables are displayed by phpinfo.php Some of commonly used predefined variables are explained here.

$_SERVER

This is an array variable that contains information about HTTP headers, script and environment. Following are some of prominent members of this array

PHP_SELF − stores filename of currently executing script. For example, a script in test folder of document root of a local server returns its path as follows −

Example

<?php
echo $_SERVER['PHP_SELF'];
?>

Output

This results in following output in browser with http://localhost/test/testscript.php URL

/test/testscript.php

SERVER_ADDR − This property of array returns The IP address of the server under which the current script is executing.

SERVER_NAME − Name of server hostunder which the current script is executing.In case of a erver running locally, localhost is returned

QUERY_STRING − A query string is the string of key=value pairs separated by & symbol and appended to URL after ? symbol. For example, http://localhost/testscript?name=xyz&age=20 URL returns trailing query string

REQUEST_METHOD − HTTP request method used for accessing a URL, such as POST, GET, POST, PUT or DELETE. In above query string example, a URL attached to query string wirh ? symbol requests the page with GET method

DOCUMENT_ROOT − returns name of directory on server that is configured as document root. On XAMPP apache server it returns htdocs as name of document root

C:/xampp/htdocs

DOCUMENT_ROOT − This is a string denoting the user agent (browser) being which is accessing the page.

Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.116 Safari/537.36

REMOTE_ADDR − IP address of machine from which the user is viewing the current page.

SERVER_PORT − port number on which the web server is listening to incoming request. Default is 80

$_GET

By default, client browser sends a request for URL on the server by HTTP GET method. A query string attached to URL may contain key=value pairs concatenated by & symbol. The $_GET associative array stores these key value pairs

Assuming that the URL in browser is http://localhost/testscript?name=xyz&age=20

Example

<?php
echo "Name : " . $_GET["name"] . "<br>";
echo "Age : " . $_GET["age"];
?>

Output

This will produce following result −

Name : xyz
Age : 20

$_POST

An associative array of key-value pairs passed to a URL by HTTP POST method that uses URLEncoded or multipart/form-data content-type in request.

Data is sent to a PHP script using POST method by specifying action=POST in a HTML form test.html as below −

<form action="testscript.php" method="POST">
<input type="text" name="name">
<input type="text" name="age">
<input type ="submit" valaue="submit">
</form>

The PHP script is as follows −

Example

<?php
echo "Name : " . $_POST["name"] . "<br>";
echo "Age : " . $_POST["age"];
?>

Output

This will produce following result −

Name : xyz
Age : 20

$_FILES

This variable is an associative array containing items uploaded via HTTP POST method. Uploading a file requires HTTP POST method form with enctype attribute set to multipart/form-data.

<form action="testscript.php" method="POST" enctype="multipart/form-data">
<input type="file" name="file">
<input type ="submit" valaue="submit">
</form>

In the PHP script, _FILES variable is accessed as follows −

Example

<?php
print_r($_FILES);
?>

Output

Array ( [file] => Array ( [name] => hello.html [type] => text/html [tmp_name] => C:\xampp\tmp\php9647.tmp [error] => 0 [size] => 56 ) )

$_REQUEST

This variable is an associative array providing contents of $_GET, $_POST and $_COOKIE predefined variables.

$_SESSION

This variable is an associative array of variables representing HTTP session.

$_ENV

An array of environment variables form this predefined variable. These variables are imported into PHP's global namespace.

$_COOKIE

Server can store certain data in client's computer in the form of cookies. These cookies are transmitted every time a request is made. The _COOKIE variable is an associative array of cookie variables and their values.

Updated on: 19-Sep-2020

195 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements