PHP (recursive acronym for PHP: Hypertext Preprocessor) is a widely-used open source general-purpose scripting language that is especially suited for web development and can be embedded into HTML -- source.
<?php ?>
End tag not needed if only PHP in file
- Letters, numbers and underscores.
- Must start with a letter.
- Case sensitive.
- Preceded by a dollar sign ($).
Number
Float
String
Boolean = true, false
Array
Are zero based for simple arrays.
$x = array();
$x = array('one', 'two', 'three');
$x = array('key' => 'value', 'another-key' => 'another-value'); = assocative array
$x = [];
$x['new-tem'] = "hello"; = sets or replaces an item
$x[] = 'add a new item to end';
$x[1]
$x[1]['index-as-string']
$games = array(
'tabletop' => array('table-tennis', 'poker')
);
+, -, *, /, ** (to power), -> (object operator)
<, >, >=, <=, == (equal), === (identical), !=
5 === '5' is false
5 == '5' is true
&&, ||, !
echo "hello $planet";
echo "show array var {$array_var['name']}"; = needs curly braces
echo 'hello world';
echo 'hello ' . 'world';
function validate_date($date_raw)
{
if ($time = strtotime($date_raw)) {
return date('m d y', $time);
}
}
asort(array) = sort an array alphabetically.
count(array) = number of items in array.
date(format, date_var) = formats dates and returns strings
dump_v(variable) = show contents of array/item etc.
echo(value) = display contents of a variable.
empty(string) = true/false - is it empty?
filter_var(variable, filter) = true/false - does item match filter e.g. FILTER_VALIDATE_EMAIL
get_file_contents(url)
htmspecialchars(string) = remove any html chars from input
implode('separator', array) = turn array into a string, separated by 'separator'.
include(file) = insert file contents. Warning if not there and program continues.
is_array(value) = true if value is an array.
json_decode(json_data, true) = create associative array. Other options.
print_r(array) = show contents of array
require(file) = include contents of file - fatal error if not there; program ends.
shuffle(array) = reorder array in a random fashion.
strtotime(string) = returns epoch date. Can also handle things like today, yesterday, last saturday of march 2014 etc. True/false if valid.
trim(string) = remove leading and trailing space from string.
var_dump(object) = display contents of object/array
if () {
//
} elseif () {
//
} else {
//
}
while () {
//
}
for($i = 0; $i < 10; $i++) {
//
}
foreach ($array as $array_item) {
//
}
foreach ($meteors as $name => $value) {
//
}
$_SERVER e.g. $_SERVER['REQUEST_METHOD'] === 'POST'
$_POST e.g. $_POST['post-var']
__DIR__ = directory of current file
- PSR - PHP Standards Recommendation
- PSR-1 = Basic Coding Standard
- PSR-2 = Coding Style Guide
- PSR-3 = Logger Standard
- PSR-4 = Auto Loading Standard
- PSR-5 = Caching Interface
- PSR-6 = HTTP Messaging Interface
- PHP-FIG = Framework Interop Group - make those standards
$substring = substr($string,$start,$length);
If $start and $length are positive, substr( ) returns $length characters in the string,
starting at $start. The first character in the string is at position 0:
print substr('watch out for that tree',6,5);
out f
If you leave out $length, substr( ) returns the string from $start to the end of the original string:
print substr('watch out for that tree',17);
t tree
If $start plus $length goes past the end of the string, substr( ) returns all of the string from $start forward:
print substr('watch out for that tree',20,5);
ree
If $start is negative, substr( ) counts back from the end of the string to determine where
your substring starts:
print substr('watch out for that tree', -6);
print substr('watch out for that tree', -17,5);
t tree
out f
If $length is negative, substr( ) counts back from the end of the string to determine
where your substring ends:
print substr('watch out for that tree',15, -2);
print substr('watch out for that tree', -4, -1);
hat tr
tre
$username = substr($_REQUEST['username'],0,8)
// make a timestamp for Jan 17 2012 at 9.34pm
$ts = mktime(21, 34, 1, 17, 2012);
echo date("d/m/y G:i:s e", $ts);
Written by Stephen Moon ([email protected]), 2016