Coding Knowledge Center
PHP
Conditional (if/else if/else) |
if () {
} else if () {
} else {
}
If/else if/else construct.
Conditional (switch) |
switch () {
case test1:
break;
case test2:
break;
case test3:
break;
default:
break;
}
Switch construct.
Configuration (ini_set) |
ini_set("session.save_path", "C:\\Windows\\Temp\\dev.mysite.com");
Sets the path session data will be written to.
When dealing with multiple sub-domains, I've had to do this to avoid session_start() permission write errors.
Constants (define - case insensitive) |
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.
Constants (define) |
define("PI", 3.14);
echo "PI equals " . PI;
Set constant variable named 'PI' to 3.14.
Constants (pre-defined) |
1) __FILE__
2) __LINE__
3) PHP_VERSION
1) File php is currently reading.
2) Current line number php is reading from file.
3) Version of php running.
Loop (break) |
break - ends loops
continue - ends iterations
Different ways to end loops.
Loop (do/while) |
do {
} while (condition);
Do/while loop construct.
Loop (for) |
for (ini; condition; mod) {
}
For loop construct.
Loop (while) |
while (condition) {
}
While loop construct.
Strings (formatting - dollar amount) |
$initial_amount = number_format($Tarray[5],2,.,,);
Formats a number into a dollar amount.
Strings (fputcsv) |
fputcsv($out, array($ein,$aba,$trustname,$ktid,$dateTrust,$initial_amount,$trustee_name));
Takes an array and combines each element into a csv string.
Variables (casting) |
$newvar = (integer) $orgvar
This is called casting. It produces a copy of $orgvar leaving the original $orgvar untouched. Here we are creating an integer version of the original variable.
Variables (set) |
$var = 1;
Set variable named 'var' to a value of 1.
Variables (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.
Variables (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.
Variables (variable functions) |
gettype($var) - get variable type
settype($var) - set variable type
These are self explanatory.