- PHP 7 Tutorial
- PHP 7 - Home
- PHP 7 - Introduction
- PHP 7 - Performance
- PHP 7 - Environment Setup
- PHP 7 - Scalar Type Declarations
- PHP 7 - Return Type Declarations
- PHP 7 - Null Coalescing Operator
- PHP 7 - Spaceship Operator
- PHP 7 - Constant Arrays
- PHP 7 - Anonymous Classes
- PHP 7 - Closure::call()
- PHP 7 - Filtered unserialize()
- PHP 7 - IntlChar
- PHP 7 - CSPRNG
- PHP 7 - Expectations
- PHP 7 - use Statement
- PHP 7 - Error Handling
- PHP 7 - Integer Division
- PHP 7 - Session Options
- PHP 7 - Deprecated Features
- PHP 7 - Removed Extensions & SAPIs
- PHP 7 Useful Resources
- PHP 7 - Quick Guide
- PHP 7 - Useful Resources
- PHP 7 - Discussion
PHP Predefined Variables
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.
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 −
<?php echo $_SERVER['PHP_SELF']; ?>
This results in following output in browser with http://localhost/test/testscript.php URL
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
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
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
<?php echo "Name : " . $_GET["name"] . "<br>"; echo "Age : " . $_GET["age"]; ?>
This will produce following result −
Name : xyz Age : 20
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 −
<?php echo "Name : " . $_POST["name"] . "<br>"; echo "Age : " . $_POST["age"]; ?>
This will produce following result −
Name : xyz Age : 20
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 −
<?php print_r($_FILES); ?>
Array ( [file] => Array ( [name] => hello.html [type] => text/html [tmp_name] => C:\xampp\tmp\php9647.tmp [error] => 0 [size] => 56 ) )
This variable is an associative array providing contents of $_GET, $_POST and $_COOKIE predefined variables.
This variable is an associative array of variables representing HTTP session.
An array of environment variables form this predefined variable. These variables are imported into PHP's global namespace.
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.
- Related Articles
- PHP Predefined Mathematical Constants
- PHP variable Variables
- PHP Variables from External Sources
- Multiple index variables in PHP foreach loop
- Predefined Identifier __func__ in C
- What are predefined attributes in C#?
- Access variables from parent scope in anonymous PHP function
- What is a predefined package in Java?
- Which is faster? Constants, Variables or Variable Arrays in PHP?
- What are the predefined functions in C language?
- Manipulate for loop to run code with specific variables only PHP?
- PHP – Retrieve internal configuration variables of iconv extension using iconv_get_encoding() function
- Can we set JOptionPane with predefined selection in Java?
- C program to calculate sum of series using predefined function