isset()
test for variable existsempty()
test for empty variablevar_dump($var)
DEBUG : displays the information of a variableheader()
send a header from the server (redirect to)is_numeric()
test to see if a value is a numberexit()
causes script top stop immediatelystr_replace('a','b', $string)
replace a with b in a stringtrim($string)
trims leading and trainling spacesexplode($separator, $string)
makes array of strings, each of which is a substring of the string parameter extracted using the separatorimplode($separator, $string)
makes the elements of an array into a stringcount($array)
count all elements from an array, or something in an objectecho $string
displays a stringprint_r($array)
displays readable information for a variablerequire 'file.php'
The include statement will issue E_ERROR if it cannot find the file, E_ERROR stops the scriptinclude 'file.php'
The include statement will issue E_WARNING if it cannot find the file, E_WARNING doesn't stop the script
if (/*condition(s) here */) {
// code here
}
=> or
if (/*condition(s) here */) {
// code here
} else {
// other code here
}
=> or
if (/*condition(s) here */) {
// code here
} elseif (/*condition(s) here */) {
// other code here
} else {
// other code here
}
Can be used with numbers, or strings
switch ($age) {
case $age < 15:
echo $age;
break;
case $age >= 15 && $age <= 25:
echo $age;
break;
case $age >= 26 && $age <= 30:
echo $age;
break;
default:
echo "Well done";
}
| Simple function
function functionName()
{
// here code
return $result;
}
| Function with parameter(s)
function functionName($param1, $param2)
{
// here code uses param1 & param2
return $result;
}
- Call function :
functionName();
- Call & print result of a function :
echo functionName();
| FOR (loop until a condition is met)
for($i = 0; $i < 5; $i++){
echo $i;
}
0
1
2
3
4
$array = ['Paris','c\'est','Gotham'];
foreach ($array as $value) {
echo $value;
}
Paris
c'est
Gotham
$i = 0;
do {
echo $i++;
} while ($i < 5)
0
1
2
3
4
- Create array :
$myArray = array();
or$myArray = [];
- Push into array :
$myArray[] = "Something";
- Push to associative array :
$myArray['key'] = "Value";
- Create numeric array :
$myArray = ['value', 'value2'];
- Create associative array :
$myArray = ['key1'=>'val1', 'key2'=>'val2'];
- Print from numeric array :
echo $myArray[0];
- Print from associative array :
echo $myArray['key'];
- Print array :
print_r($myArray);
- Tips :
- For associative array, keys are strings (example : 'firstname', 'lastname', 'email',...)
- For numeric array, keys are numbers (Always 0, 1, 2...)