Contents / Previous / Next


Variables

Syntax

All variables are identified by using the $ sign as a prefix, followed by a letter, then more letters or numbers including the underscore.

Automatic Typing

PHP variable type is automatically determined by the way a value is assigned to it.
Supported variables include integers, floating point numbers, strings, arrays, and objects.

Reference

Values to variables can be assigned by reference.
This means that the new variable simply references (in other words, "becomes an alias for" or "points to") the original variable. Changes to the new variable affect the original, and vice versa.
To assign by reference, simply prepend an ampersand (&) to the beginning of the variable which is being assigned (the source variable): <?php $foo = 'Bob'; // Assign the value 'Bob' to $foo $bar = &$foo; // Reference $foo via $bar. ?>

Variable scope

The scope of a variable is the context within which it is defined.
Any variable defined inside a function is by default limited to the local function scope.

Global variables must be declared global inside a function if they are going to be used in that function:

$a = 1; $b = 2; function Sum () { global $a, $b; $b = $a + $b; } Sum (); echo $b; The above script will output "3".
A second way to access variables from the global scope is to use the special PHP-defined $GLOBALS array. The previous example can be rewritten as: Function Sum () { $GLOBALS["b"] = $GLOBALS["a"] + $GLOBALS["b"]; }

Static variables have a scope limited to one function but their value is not forgotten from one function call to an other:

function static_test () { static $count = 0; $count++; echo $count; if ($count < 10) { static_test (); } $count--; } This function recursively counts to 10, using the static variable $count to know when to stop.

Variable variables have a variable name which can be set and used dynamically. It takes the value of an other variable and treats that as its name. Example:

$a = "hello"; // normal variable $$a = "world"; // The following echos both produce: hello world. echo "$a ${$a}"; echo "$a $hello"; In order to use variable variables with arrays, you have to resolve an ambiguity problem. That is, if you write $$a[1] then the parser needs to know if you meant to use $a[1] as a variable, or if you wanted $$a as the variable and then the [1] index from that variable. The syntax for resolving this ambiguity is: ${$a[1]} for the first case and ${$a}[1] for the second.

Predefined Variables

The phpinfo() function may be used to get a list of predefined variables.
The environment variables listed in phpinfo() can be used to gather information about browsers, clients, IP addresses, emails, and other information.
They may for example be useful to track visitors to a web site combined with the use of a database or to add extra authentication.

Apache variables

$SERVER_NAME

The name of the server host under which the current script is executing.

$REQUEST_METHOD

Which request method was used to access the page; i.e. 'GET', 'HEAD', 'POST', 'PUT'.

$QUERY_STRING

The query string, if any, via which the page was accessed.

$DOCUMENT_ROOT

The document root directory under which the current script is executing, as defined in the server's configuration file.

$HTTP_HOST

Contents of the Host: header from the current request, if there is one.

$HTTP_USER_AGENT

This is a string denoting the browser software being used to view the current page.

$REMOTE_ADDR

The IP address from which the user is viewing the current page.

PHP Variables These variables are created by PHP itself.
$argv

Array of arguments passed to the script. When the script is run on the command line, this gives C-style access to the command line parameters. When called via the GET method, this will contain the query string.

$argc

Contains the number of command line parameters passed to the script (if run on the command line).

$PHP_SELF

The filename of the currently executing script, relative to the document root.

$HTTP_COOKIE_VARS

An associative array of variables passed to the current script via HTTP cookies.

$HTTP_GET_VARS

An associative array of variables passed to the current script via the HTTP GET method.

$HTTP_POST_VARS

An associative array of variables passed to the current script via the HTTP POST method.

$HTTP_POST_FILES

An associative array of variables containing information about files uploaded via the HTTP POST method.

$HTTP_ENV_VARS

An associative array of variables passed to the current script via the parent environment.

$HTTP_SERVER_VARS

An associative array of variables passed to the current script from the HTTP server. These variables are analogous to the Apache variables described above.

Data Types

Datatypes recognized by PHP:
Integer: For assigning an integer value, three different types of notation can be used: decimal (regular base 10), hexadecimal (base 16), or octal (base 8): $myint = 83; // Normal decimal $myint = O123; // Octal for # 83 $myint = 0x53; // Hexadecimal for # 83


Floating-Point Number 'Double': Floating-point numbers can be expressed in two different types of notation:

$myfloat = 1.234; // Standard decimal $myfloat = .001234e3; // Scientific


String: A string is series of characters. In PHP, a character is the same as a byte, that is, there are exactly 256 different characters possible. This also implies that PHP has no native support of Unicode.

There is no bound to the size of strings imposed by PHP.

A string value must begin and end with a pair of either single (' ') or double (" ") quotes:

$myint = 10; $string_two = "The value of myint is $myint"; $string_one = 'The name of myint is $myint';
If the string is enclosed in double-quotes (""), PHP understands more escape sequences for special characters: \n linefeed (LF or 0x0A (10) in ASCII) \r carriage return (CR or 0x0D (13) in ASCII) \t horizontal tab (HT or 0x09 (9) in ASCII) \\ backslash


Check the Type of Variables

The gettype() function, takes as it's argument the variable, and returns a string of the type of variable (like 'integer', 'string', 'double', etc.): [$strVarType]=gettype([variable name]) And $strVarType will contain the string Checking Specific Data types

It is also possible to check whether a variable is of a specific type, the following PHP functions take the variable as an argument, and return a boolean:

Changing Data type ('Typecasting')

Typecasting is performed with the type written in parenthesis as follows: $i = (int) $myrealvalue;

The settype() function can also be used to convert one data type to another.