Skip to content
Jonathan Daggerhart edited this page Sep 21, 2015 · 1 revision

System Variables and Magic Constants

PHP provides a few global variables and constants that are very useful when writing programs. They help the developer understand the visitor's request, and the context in which the execution of scripts occur.

System Variables

System variables provide information about the page the user is visiting, data the visitor is requesting, data the visitor has submitted, and data about the server the program is running on.

$_GET

The $_GET variable contains values that are included in the url of the request.

Assume we are visiting the url - index.php?some_key=some_value&another_key=another_value

<?php 
print_r( $_GET );

Outputs:

Array
(
    [some_key] => some_value
    [another_key] => another_value
)

$_POST

The $_POST variable works almost exactly like the $_GET variable, except values are not sent by url, but rather as part of the http request. The result being that the values are hidden from the browser's history.

A form was submitted with a HTML input tags with the names "email", "name", and "message":

<?php 
print_r( $_POST );

Outputs:

Array
(
    [email] => someone@example.com
    [name] => Charlie
    [message] => How's it going?
)

$_COOKIE

The $_COOKIE variable contains the values of cookies stored in the visitor's browser for this domain.

<?php 
setcookie( 'some_cookie_key', 'My cookie value' );
print_r($_COOKIE);

Outputs:

Array
(
    [some_cookie_key] => My cookie value
)

$_REQUEST

The $_REQUEST array contains the values of $_GET and $_POST.

Note: I don't recommend using this variable unless you're sure you need it. Since it contains the values from multiple sources, collisions could occur.

Note 2: $_REQUEST can contain the value of $_COOKIE as well, depending on server configuration. All the more reason not to use it.

$_SERVER

The $_SERVER variable contains data about the server, the file being executed, and the request.

print_r( $_SERVER );

Outputs something like:

Array
(
    [APP_ENV] => dev
    [HTTP_HOST] => academy.dev
    [HTTP_CONNECTION] => keep-alive
    [HTTP_PRAGMA] => no-cache
    [HTTP_CACHE_CONTROL] => no-cache
    [HTTP_ACCEPT] => text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8
    [HTTP_UPGRADE_INSECURE_REQUESTS] => 1
    [HTTP_USER_AGENT] => Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/45.0.2454.85 Safari/537.36
    [HTTP_REFERER] => http://academy.dev/Understanding-PHP/game-arrays/index.php
    [HTTP_ACCEPT_ENCODING] => gzip, deflate, sdch
    [HTTP_ACCEPT_LANGUAGE] => en-US,en;q=0.8
    [PATH] => /sbin:/usr/sbin:/bin:/usr/bin
    [SERVER_SIGNATURE] => 
    [SERVER_SOFTWARE] => Apache/2.2.15 (CentOS)
    [SERVER_NAME] => academy.dev
    [SERVER_ADDR] => 192.168.56.102
    [SERVER_PORT] => 80
    [REMOTE_ADDR] => 192.168.56.1
    [DOCUMENT_ROOT] => /var/www/dev.dev/academy.dev/www
    [SERVER_ADMIN] => [no address given]
    [SCRIPT_FILENAME] => /var/www/dev.dev/academy.dev/www/Understanding-PHP/game-arrays/index.php
    [REMOTE_PORT] => 58050
    [GATEWAY_INTERFACE] => CGI/1.1
    [SERVER_PROTOCOL] => HTTP/1.1
    [REQUEST_METHOD] => GET
    [QUERY_STRING] => choice=nature
    [REQUEST_URI] => /Understanding-PHP/game-arrays/index.php?choice=nature
    [SCRIPT_NAME] => /Understanding-PHP/game-arrays/index.php
    [PHP_SELF] => /Understanding-PHP/game-arrays/index.php
    [REQUEST_TIME_FLOAT] => 1441681378.202
    [REQUEST_TIME] => 1441681378
)

Note: We can see the visitor's IP address (REMOTE_ADDR), browser details (HTTP_USER_AGENT), and where they came from (HTTP_REFERER).

echo $_SERVER['REMOTE_ADDR'];
// 192.168.56.1

Magic Constants

In PHP, "Magic" constants provide some useful contextual information about the location in which they are called.

__FILE__

The absolute server side path to the current PHP file that is being executed.

echo __FILE__;
/*
/var/www/dev.dev/academy.dev/www/Understanding-PHP/game-arrays/index.php
*/

__DIR__

The server site absolute directory for the current PHP file that is being executed.

echo __DIR__;
/*
/var/www/dev.dev/academy.dev/www/Understanding-PHP/game-arrays
*/

Note: Path does not have trailing slash.

Others

  • __FUNCTION__ - current function being executed.
  • __CLASS__ - current class being executed.
  • __METHOD__ - current class method being executed.