4-4: PHP programming language basics – predefined variables

PHP provides access to a number of predefined functions and variables.

For an exhaustive overview of the predefined variables available in the PHP programming language please check out the related section on php.net website, from which the following complete list was derived.

A complete list of PHP predefined variables according to the php.net website

Superglobals — Superglobals are built-in variables that are always available in all scopes
$GLOBALS — References all variables available in global scope
$_SERVER — Server and execution environment information
$_GET — HTTP GET variables
$_POST — HTTP POST variables
$_FILES — HTTP File Upload variables
$_REQUEST — HTTP Request variables
$_SESSION — Session variables
$_ENV — Environment variables
$_COOKIE — HTTP Cookies
$php_errormsg — The previous error message
$HTTP_RAW_POST_DATA — Raw POST data
$http_response_header — HTTP response headers
$argc — The number of arguments passed to script
$argv — Array of arguments passed to script

In this section we focus on a couple of those built-in variables that we may use in our scripts. In particular we will see how the $_SERVER array provides access to information on our working environment and we will just hint, for now, at the $_POST array, that let us retrieve data submitted by website visitors through web forms, an essential bit of the web applications that we will build later on.

The $_SERVER variable

The $_SERVER variable, an array with several predefined keys, provides access to many so called “environmental” variables about our working environment and our website visitors.

To check all available keys for the $_SERVER array, we can use the array_keys() function that we have introduced in the previous section about PHP arrays basics.

At the time of this writing, the code above will output the following result on our test Linux server:

  • HTTP_HOST
  • HTTP_CONNECTION
  • HTTP_UPGRADE_INSECURE_REQUESTS
  • HTTP_USER_AGENT
  • HTTP_ACCEPT
  • HTTP_ACCEPT_ENCODING
  • HTTP_ACCEPT_LANGUAGE
  • PATH
  • SERVER_SIGNATURE
  • SERVER_SOFTWARE
  • SERVER_NAME
  • SERVER_ADDR
  • SERVER_PORT
  • REMOTE_ADDR
  • DOCUMENT_ROOT
  • REQUEST_SCHEME
  • CONTEXT_PREFIX
  • CONTEXT_DOCUMENT_ROOT
  • SERVER_ADMIN
  • SCRIPT_FILENAME
  • REMOTE_PORT
  • GATEWAY_INTERFACE
  • SERVER_PROTOCOL
  • REQUEST_METHOD
  • QUERY_STRING
  • REQUEST_URI
  • SCRIPT_NAME
  • PHP_SELF
  • REQUEST_TIME_FLOAT
  • REQUEST_TIME

You can find the meaning of each one of these keys here. Let’s focus on a few of those.

The general syntax for retrieving the value for each one of these keys is the usual syntax for array with keys: the array name followed by the name of the key enclosed in square brackets:

SERVER_ADDR

the SERVER_ADDR key returns the IP address of the server on which the script is running, the web server:

REMOTE_ADDR

the REMOTE_ADDR key returns the IP address of the visitor’s computer, where the web browser is running:

DOCUMENT_ROOT

the DOCUMENT_ROOT key returns the path of the web server’s web root, as defined in the Apache configuration file.

SCRIPT_NAME

The SCRIPT_NAME key returns the path of the script currently running.

As an exercise, try running the following script on you own web server and check out the results!

The $_POST variable

The $_POST variable is, as $_SERVER, an array with keys. However, unlike $_SERVER, the keys are not predefined but rather created “on the fly” when the user presses the “Submit” button of a web form. After the user submits the data from a web form, the $_POST array gets populated with one key for each of the different fields that were present in the web form. We will go through these mechanisms in great detail on the next chapter. For now, let us just say that each field of a web form is associated with a “name” attribute. For example if a user submits a form that contains fields with name=”user” and name=”sequence”, the user and sequence keys will be generated in the $_POST array, and will point to the values given to these two fields within the web form. We can then, within the PHP script, collect these values:

So in short, $_POST is used by a PHP script to collect data from a submitted web form. This will become more clear on reading the next chapter.

Chapter Sections

[pagelist include=”435″]

[siblings]

One thought on “4-4: PHP programming language basics – predefined variables”

  1. Great reference. I was looking for some additional information not listed on php.net

    You’re missing a few. Not sure if you wanted a complete list. HTTP_X_FORWARDED_FOR

    Thanks

Leave a Reply

Your email address will not be published. Required fields are marked *