PHP Predefined variables.

There are a number of pre-defined variables in PHP. See http://php.net for a cmoplete listing. Below is a subset of predefined variables/arrays :

 

Variable Meaning
$GOBALS

arrray referencing the variables accessible in all scopes:

<?php
function test() {
    $foo = "local variable";

    echo '$foo in global scope: ' . $GLOBALS["foo"] . "\n";
    echo '$foo in current scope: ' . $foo . "\n";
}

$foo = "Example content";
test();
?>
$_SERVER

array containing defining information about the PHP Server.

i.e.

  • $_SERVER['PHP_SELF'] = gives file name of script that is executing
  • $_SERVER['SERVER_ADDR'] = ip address of host running PHP server.
  • $_SERVER[ 'REQUEST_METHOD'] = CGI request method used to invoke the php script. Example - GET, POST,etc.

 

OTHER keys include (see http://php.net for complete list):

  • SERVER_NAME, SERVER_SOFTWARE, SERVER_PROTOCOL, REQUEST_TIME, QUERY_STRING, DOCUMENT_ROOT, HTTP_ACCEPT, HTTP_ACCEPT_ENCODING, HTTP_ACCEPT_LANGUAGE, HTTP_HOST, HTTP_REFERER, HTTP_USER_AGENT, REMOTE_ADDR, REMOTE_HOST, SERVER_SIGNATURE, REQUEST_URI, PHP_AUT_USER, PHP_AUTH_PW, AUTH_TYPE

 

$_GET

array containing the variables passed via an HTTP GET method request

i.e. $name = $_GET["thename"];

//where "thename" is the variable name sent in the request and $_GET["thename"] is the corresponding value.

NOTE: often you may want to use the PHP function htmlspecialchars which will parse the variable information for html encoding

i.e. $name = htmlspecialchars($_GET["thename"]);

$_POST

array containing values past via an HTTP POST method request.

 

i.e. $name = $_POST["thename"];

//where "thename" is the variable name sent in the request and $_POST["thename"] is the corresponding value.

NOTE; see $_GET for discussion on html special characters

$_FILES

array used to contain file information when have through post done file uploads. See our example for file uploads.

Below, if you have a file upload using an <input type=file name=userfile> the following elements are defined:

$_FILES['userfile']['name']

The original name of the file on the client machine.

$_FILES['userfile']['type']

The mime type of the file, if the browser provided this information. An example would be "image/gif". This mime type is however not checked on the PHP side and therefore don't take its value for granted.

$_FILES['userfile']['size']

The size, in bytes, of the uploaded file.

$_FILES['userfile']['tmp_name']

The temporary filename of the file in which the uploaded file was stored on the server.

$_FILES['userfile']['error']

The error code associated with this file upload. This element was added in PHP 4.2.0

 

   
   
   
   

 

© Lynne Grewe