Skip to content

Latest commit

 

History

History
197 lines (164 loc) · 4.55 KB

PHP.md

File metadata and controls

197 lines (164 loc) · 4.55 KB

PHP

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.

Code Blocks Tags

<?php ?>
End tag not needed if only PHP in file

Variable Names

  • Letters, numbers and underscores.
  • Must start with a letter.
  • Case sensitive.
  • Preceded by a dollar sign ($).

Main Types

Number
Float
String
Boolean = true, false
Array

Arrays

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')
);

Operators

+, -, *, /, ** (to power), -> (object operator)

Comparison Operators

<, >, >=, <=, == (equal), === (identical), !=
5 === '5' is false
5 == '5' is true

Logical Operators

&&, ||, !

String Usage

echo "hello $planet";
echo "show array var {$array_var['name']}"; = needs curly braces
echo 'hello world';
echo 'hello ' . 'world';

Functions (Defined)

function validate_date($date_raw)
{
  if ($time = strtotime($date_raw)) {
    return date('m d y', $time);
  }
}

Functions (Inbuilt)

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

Conditionals and Loops

if () {
//
} elseif () {
//
} else {
//
}

while () {
//
}

for($i = 0; $i < 10; $i++) {
//
}

foreach ($array as $array_item) {
//
}

foreach ($meteors as $name => $value) {
//
}

Super Globals

$_SERVER e.g. $_SERVER['REQUEST_METHOD'] === 'POST'
$_POST e.g. $_POST['post-var']

Magic Constants

__DIR__ = directory of current file

Acronyms

  • 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

Accessing Substrings

$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)

Creating Timestamps

// 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