PHP Language

Data Types

integer, float, boolean, string, array, object, resource, NULL

Variable Declarations

$variablename = <value>; //type defined by type of <value>
$anothervariable =& $variablename; //Assign by Reference

Array

$arrayname = array();
$arrayname = array(<value1> [, <value2>, ...]); //Initialized
$arrayname = array(<key> => <value> [, <key> => <value>, ...]); //Define Keys
$multiarray = array(<key> => array(<value1> [, <value2>]) [, <key> => ...]); //Multi-dimensional

Common Array Functions

array_push(<array>,<value>); //Push item onto end of array
array_pop(<array>); //Pop item off end of array
[a][r]sort(<array>); //Sort array, "a" assigns new keys, "r" reverses
count(<array>[,COUNT_RECURSIVE]); //Count elements, use COUNT_RECURSIVE for multidimensional array

Comments

// Comment text
/* Multi-line comment text */
# Comment text

Operators

Arithmetic: + (Addition), - (Subtraction), * (Multiplication), / (Division), % (Modulus)

Relational: == (Equal), === (Equal with type comparison), != (Not equal), <> (Not equal), !== (Not Equal with type comparison), < (Less than), > (Greater than), <= (Less than or equal to), >= (Greater than or equal to), str[case]cmp(<string1>,<string2>); (Binary safe string comparison, insert "case" for case-insensitive)

Logical: ! (logical NOT), && (logical AND), || (logical OR), xor (logical XOR)

Assignment: = (Assign), += (Addition), -= (Subtraction), *= (Multiplication), /= (Division), .= (Append), . (Concatenation), %= (Modulus), &= (And), |= (Or), ^= (Exclusive Or), <<= (Left Shift), >>= (Right Shift)

String

<string> . <string> //Concatenation; seperate strings with period)
substr(<string>, <start>[, <length>]); // Returns the part of <string> starting at <start> for <length> characters.
strlen(<string>); // Return length of string
str_replace(<search>,<replace>,<string>[,<count>]); // substitue <count> (default: all) occurances of <search> with <replace> in <string>
strpos(<string>, <search>); //Find <search> in <string>, return index.
[l|r]trim(<string>); // Trim spaces; add "l" to trim only left, "r" to trim only right.
strto[lower|upper](<string>); // Convert string to all upper or lower case
str[case]cmp(<string1>,<string2>); //Binary safe string comparison, insert "case" for case-insensitive
explode(<delim>,<string>,[<limit>]); //Split <string> into array of up to <limit> elements as delimited by <delim>
implode(<delim>,<array>); //Join <array> into string separated by <delim>

Statements

Flow

if (<condition>) { <statements>; } elseif (<condition>) { <statements>; } else { <statements>; }
<condition> ? <truevalue> : <falsevalue>;
for (<init stmt>;<condition>;<update stmt>) { <statements>; }
foreach (<array> as [<value> | <key> => <value>]) { <statements>; [break;] [continue;] }
while (<condition>) { <statements>; }
do {
<statements>; } while (<condition>);
switch (
<expression>) { case <literal or type>: <statements>; [break;] [case...] default: <statements>; }

function <function_name>([<parameters>]) { <statements>; [return <value>;] }

Classes

class <class_name> [extends <base_class>] {
 [var [<modifiers>] <class member variables>;]
// Modifiers [public | private | static] are implemented in PHP5
 function <class_name>([<constructor_parameters>]) { <statements>; }

  // a class function with a name equal to the class is a constructor for the class and will be called at the point that a new object is instantiated.
 function <class_function_name>([<parameters>]) { <statements>; }
 }

$object = new class_name([<constructor_parameters>]);
$object->class_function_name([<parameters>]);
class_name::class_function_name([<parameters>]); //Static call
$object->class_var //Reference a variable inside a class object. Note there is no $ in front of class_var.
  //In PHP5, private class vars can not be accessed.

Error Handling

try {
	<statements that may cause error>;
	}
catch (<Exception Class> $exception_name) {
	<statements to execute when error is caught>;
	}

Input / Output

Cookies

setcookie (<cookiename>, [<value>],[<expire_time_in_secs_since_epoch>]);

$_COOKIE['cookiename']; (Returns value of cookie)

Sessions

session_start(); (Create session)

$_SESSION['key_name'] = value; (Set session variable)

$variablename = $_SESSION['key_name']; (Retrieve value from session variable)

session_destroy(); (Destroy session)

Globals

$GLOBALS (Access all global variables in script)

$_SERVER (Access web server variables)

$_GET (Values passed to script through URL)

$_POST (Values passed to script through HTTP Post)

$_COOKIE (Values passed by user cookie)

$_FILES (Values passed by HTTP Post File Uploads)

$_ENV (Values passed to script via the environment)

$_REQUEST (Values passed by URL, HTTP Post, or user Cookies)

$_SESSION (Values passed through user's session)

See also: