Php Reference Sheet

ADVERTISEMENT

010010100101011010101101001010101001011101011010
Data Types
Super Globals
integer, float, boolean, string, array, object, resource, NULL
$GLOBALS (Access all global variables in script)
$_SERVER (Access web server variables)
Variable Declarations
$_GET (Values passed to script through URL)
$variablename = <value>;
$_POST (Values passed to script through HTTP Post)
$anothervariable =& $variablename; (Assign by Reference)
$_COOKIE (Values passed by user cookie)
$_FILES (
$_FILES (Values passed by HTTP Post File Uploads)
Declare Array
$_ENV (Values passed to script via the environment)
$arrayname = array();
$arrayname = array();
$_REQUEST (Values passed by URL, HTTP Post, or user Cookies)
$_SESSION (Values passed through user's session)
Initialize Array
$arrayname = array(<value1>, <value2>, <value3>);
If Else
$arrayname = array(<key> => <value>, <key> => <value>); (Define Keys)
if (<condition 1>)
$multiarray = array(<key> => array(<value1>,<value2>)); (Multi-dimensional)
{ <statement 1>; }
elseif (<condition 2>)
elseif (<condition 2>)
Common Array Functions
{ <statement 2>; }
sort(<array>); (Sort array assigns new keys)
else
asort(<array>); (Sort array maintain keys)
asort(<array>); (Sort array maintain keys)
{ <statement 3>; }
rsort(<array>); (Sort array in reverse, new keys)
arsort(<array>); (Sort array in reverse, maintain keys)
Inline If (Ternary)
count(<array>); (Count elements)
<condition> ? true : false;
count(<array>,COUNT_RECURSIVE); (Count multidimensional array)
array_push(<array>,<value>); (Push item onto end of array)
For Loop
array_pop(<array>); (Pop item off end of array)
for (<initialize>;<condition>;<update>)
for (<initialize>;<condition>;<update>)
{
Comments
Comments
<statements>;
// Comment text
}
/* Multi-line comment text */
# Comment text
For Each Loop
foreach (<array> as [<value> |<key> => <value>])
Arithmetic Operators
{
+ (Addition), - (Subtraction), * (Multiplication), / (Division), % (Modulus)
<statements>;
<statements>;
[break];
Relational Operators
[continue];
== (Equal), === (Equal with type comparison), != (Not equal), <> (Not equal), !== (Not Equal
== (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)
While Loop
while (<condition>)
Logical Operators
{
! (logical NOT), && (logical AND), || (logical OR), xor (logical XOR)
<statements>;
<statements>;
}
Assignment Operators
= (Assign), += (Addition), -= (Subtraction), *= (Multiplication), /= (Division), .=
= (Assign), += (Addition), -= (Subtraction), *= (Multiplication), /= (Division), .=
Do-While Loop
(Concatenation), %= (Modulus), &= (And), |= (Or), ^= (Exclusive Or), <<= (Left Shift), >>=
do
(Right Shift)
{
<statements>;
String Concatenation
} while (<condition>);
. (Period)
Switch
Switch
String Manipulation
switch (<expression>)
substr(<string>,<start>,[<length>]);
{
strlen(<string>);
strlen(<string>);
case <literal or type>:
trim(<string>);
<statements>;
ltrim(<string>); // Trim left
[break;]
rtrim(<string>); // Trim right
case <literal or type>:
strtolower(<string>);
<statements>;
<statements>;
strtoupper(<string>);
[break;]
str_replace(<search>,<replace>,<string>,[<count>]);
default:
strpos(<string>, <search>);
strpos(<string>, <search>);
<statements>;
strcmp(<string1>,<string2>); (Binary safe string comparison)
}
strcasecmp(<string1>,<string2>); (Binary safe case-insensitive comparison)
explode(<delim>,<string>,[<limit>]); (Break string into array)
Function Structure
implode(<delim>,<array>); (Join array into string separated by delim)
function <function_name>([<parameters>])
function <function_name>([<parameters>])
{
Cookies
<statements>;
setcookie (<cookiename>, [<value>],[<expire_time_in_secs_since_epoch>]);
[return <value>;]
$_COOKIE['cookiename']; (Returns value of cookie)
$_COOKIE['cookiename']; (Returns value of cookie)
}
Sessions
Class Structure
session_start(); (Create session)
class <class_name> [<extends base_class>]
$_SESSION['key_name'] = value; (Set session variable)
{ {
$variablename = $_SESSION['key_name']; (Retrieve value from session variable)
[var | <modifiers*>] [<class member variables>];
session_destroy(); (Destroy session)
[<modifiers*>] function <function_name>([<parameters>])
{
Error Handling
<statements>;
try {
try {
}
<statements that may cause error>;
}
}
catch (<Exception Class> $exception_name)
* Modifiers <public | pr ivate | static> are implemented in PHP5
{
<statements to execute when error is caught>;
Declare and Use Class
}
$variable = new class_name();
$variable->function_name();
class_name::function_name(); (Static call)
Download More Reference Sheets & Get Programming Help @
Edited By: hotsnoj, Martyr2
Published: October 18, 2007

ADVERTISEMENT

00 votes

Related Articles

Related forms

Related Categories

Parent category: Education
Go