Cheat Sheet – PHP

PHP Conditions & Loops

if (condition) { action }
elseif (condition) { action }
else { action }

switch (1st half of condition) {
case “2nd half of condition”:
action
break;
default:
action
break;
}

while (condition) {
action
increment condition variable
}

do {
action
increment condition variable
}
while (condition);

for (initialize; condition; increment)
{ action to take }

PHP Arrays

array(“value0″, “value1″);
array(“key0″ => “value0″);
foreach (array as value) { action }

PHP Syntax

Opening/Closing Tags: <?php   ?>
Instruction Termination: ;
Comments: /*   */   or   //   or   #

PHP Variables

$example_1 = 789;
$example_2 = ‘value';
$example_3 = “value”;

PHP Echo

echo ‘string';
echo “string”;
echo $variable;

PHP Strings

Escape Character: \
Concatenation Operator: .
Concatenation Assignment: .=

PHP Operators

Assignment: =
Arithmetic: +   –   *   /   %
Inc./Dec.: ++   —
Logical: &&   ||   !
Comparison:
==   !=   <>   >   <   >=   <=

Functions

time(); // Current Unix Timestamp
date(); // Formats Date/Time
strtotime(); // Parses to Timestamp
include(“”);
require(“”);
mail(to,subject,message);

User-Defined Functions

function name() { }
name();

Predefined Variables

$_GET
$_POST
$_FILES
$_SERVER
$_COOKIE
$_SESSION
$_REQUEST
$GLOBALS
$php_errormsg

Browser Interaction

session_start();
session_destroy();
setcookie(name, value, expiration);

File Functions

fopen(filename, method);
feof(filehandle);
fgets(filehandle);
filesize(filename);
fread(filehandle, filesize);
fwrite(filehandle, filedata);
fclose(filehandle)
unlink(filename);

GD Library Image Functions

header()
imagecreate()
imagecreatetruecolor()
imagecolorallocate()
imagefilltoborder()
imagejpeg(), imagegif(), imagepng()
imagedestroy()
imagecreatefromgif()
imagecreatefromjpeg()
imagecreatefrompng()
imagettftext()
imagefilter()
imagecopyresampled()

Comments