Predefined Variables

0 bettenanson4178 December 12, 2020

PHP provides a large number of predefined variables to all the code or scripts or projects. PHP also provides a predefined array containing variables that depend upon the server. Here, we will discuss most of the predefined variables.

All the variables we will discuss below are easily available with every scope.

PHP Superglobal Variables

Superglobal are those variables which are access in every where with any scope.

$GLOBALS Variables

An array containing all the variables that is can be accessed globally inside the script. The key of the array is the variable name.

<?php 

function website(){
    echo "Welcome to : ".$GLOBALS["website"];
   echo "Welcome to : ".$website;
}

?>

The output of the above program is:

Welcome to : Example Content
Welcome to : Example Content

Note: There is no need to declare the global $variable; to access inside the functions or methods. It can be access any where at nay time.

$SERVER Variables

This is the array containing information such as headers, scripts location, and path. This array is created by the server so this array may be different in different servers.

There are some keys that you can access using $SERVER variables.

SNKey and Description
1PHP_SELF
This is the key result of the currently executing script relative to the root document. For Example, if the $SERVER[‘PHP_SELF’] variable runs in https://codehelppro.com/detail/php/predefined-variables.php then the result would be /detail/php/predefined-variables.php
2SERVER_ADDR
Results IP address of where script currently running.
3SERVER_HOST
This will return the server name of where the script currently running. It is usually a domain or sub-domain name.
4REQUEST_METHOD
Return the request method to access the page. eg: GET, POST, etc.
5REQUEST_TIME
This is the key that will result in the timestamp of the start of the request.
6DOCUMENT_ROOT
Results the root document path under which the script is running.
7HTTP_ACCEPT
If there is a current request then it returns the contents of the request: header.
8HTTP_ACCEPT_CHARSERT
If there is a current request then it returns the contents of the Accept-Charset: header.
9HTTP_ACCEPT_ENCODING
If there is a current request then it returns the contents of the Accept-Encoding: header. Eg: ‘gzip’
10HTTP_ACCEPT_LANGUAGE
If there is a current request then it returns the contents of the Accept-Language: header. Eg: ‘en’
11HTTP_CONNECTION
If there is a current request then it returns the contents of the Connection: header. Eg: ‘Keep Alive’
12HTTP_HOST
If there is a current request then it returns the contents of the Host: header.
13HTTP_REFERER
This will return the referred user agent to the current page.
14HTTP_USER_AGENT
If there is a current request then it returns the contents of the User-Agent: header.
15HTTPS
If you are requesting through an HTTPS connection, then it will return a non-empty string.
16SCRIPT_ADMIN
This will returns the current script’s path.
17REQUEST_URI
This returns a URL to access this page. For example index.html
18SERVER_PORT
This will return the port of the currently running script.
$SERVER Variables Key

$GET Variables

This is the collection of array that is passed through the URL. We will learn this variable in details in next chapter.

$POST Variables

This is the collection of the array that is passed through the HTTP POST Request. We will learn this variable in detail in the next chapter.

$FILES Variables

This is the HTTP file upload variables. It is the collection of the array that is passed through the HTTP POST request for uploading files.

Let’s assume you have the html form as given below.

<form method="post" action="upload.php" enctype="multipart/form-data">
    <input type="file" name="image" />
    <input type="submit" value="Upload Image" />
</form>

If we submit a post request after uploading the file then we will get the following sample array. To get that array, We have to print the $_FILES variables.

Array
(
      [name] => MyFile.txt (comes from the browser, your uploaded file name)
      [type] => text/plain
      [tmp_name] => /tmp/php/php1h4j1o (from your system)
      [error] => UPLOAD_ERR_OK  (= 0)
      [size] => 123   (the size in bytes)
)

You have to use the above array to upload the file on your server.

$REQUEST Variable

This is the collection of the array that contents the variables of $_GET, $_POST, and $_COOKIE variables. You can access all of these variable’s values using a $_REQUEST variable.