PHP
[top]
Conditional Statements
If/else if/else
if () {
}
if () {
} else {
}
if () {
} else if () {
} else {
}
If/else constructs.
Switch
switch () {
case test1:
break;
case test2:
break;
case test3:
break;
default:
break;
}
Switch construct.
While loop
while (condition) {
}
While loop construct.
Do/while loop
do {
} while (condition);
Do/while loop construct.
For loop
for (ini; condition; mod) {
}
For loop construct.
Terminate Loops
break - ends loops
continue - end interations
Different ways to end loops.
[top]
Constants
Define Constants
define("PI", 3.14);
echo "PI equals " . PI;
Set constant variable named 'PI' to 3.14.
define("PI", 3.14, true);
echo "PI equals " . pi;
Set constant variable named 'PI' to 3.14 but in this case the variable name is not case sensitive.
Predefined Constants
__FILE__
File php is currently reading.
__LINE__
Current line number php is reading from file.
PHP_VERSION
Version of php running.
[top]
Superglobals
$_GET - variables provided using GET method
$_POST - variables provided using POST method
$_COOKIE - variables provided using cookies
$_FILES - variables provided using uploads
$_SERVER - info such as headers, file paths and script locations
$_ENV - variables provided as server environment
$_REQUEST - variables provided through user input
$_SESSION - variables currently registered in a session
These are all superglobals provided by php.
[top]
Variables
Set Variable
$var = 1;
Set variable named 'var' to a value of 1.
Variable Types
boolean true/false
integer - whole number
floar or double - floating point number
string - character collection
object - instance of a class
array - ordered set of keys/values
resource - database
NULL - uninitialized variable value
These are the various variable types.
Variable Functions
gettype($var) - get variable type
settype($var) - set variable type
These are self explanatory.
Casting
$newvar = (integer) $orgvar
This is called casting. It produces a copy of $orgvar leaving the original $orgvar untouched.
Cheatsheets






