mkormendy
1/21/2016 - 5:26 AM

PHP Super Cheatsheet

PHP Super Cheatsheet

##PHP Array Functions

array_diff (arr1, arr2 ...)

array_filter (arr, function)

array_flip (arr)

array_intersect (arr1, arr2 ...)

array_merge (arr1, arr2 ...)

array_pop (arr)

array_push (arr, var1, var2 ...)

array_reverse (arr)

array_search (needle, arr)

array_walk (arr, function)

count (count)

in_array (needle, haystack)

##PHP String Functions

crypt (str, salt)

explode (sep, str)

implode (glue, arr)

nl2br (str)

sprintf (frmt, args)

strip_tags (str, allowed_tags)

str_replace (search, replace, str)

strpos (str, needle)

strrev (str)

strstr (str, needle)

strtolower (str)

strtoupper (str)

substr (string, start, len)

##PHP Filesystem Functions

clearstatcache ()

copy (source, dest)

fclose (handle)

fgets (handle, len)

file (file)

filemtime (file)

filesize (file)

file_exists (file)

fopen (file, mode)

fread (handle, len)

fwrite (handle, str)

readfile (file)

##PHP Date and Time Functions

checkdate (month, day, year)

date (format, timestamp)

getdate (timestamp)

mktime (hr, min, sec, month, day, yr)

strftime (formatstring, timestamp)

strtotime (str)

time ()

##PHP Regular Expres­sions Functions

ereg (pattern, str)

split (pattern, str)

ereg_replace (pattern, replace, str)

preg_grep (pattern, arr)

preg_match (pattern, str)

preg_match_all (pattern, str, arr)

preg_replace (pattern, replace, str)

preg_split (pattern, str)

##Regular Expres­sions Syntax

charactersdescription
^Start of string
$End of string
.Any single character
(a\b)a or b
(...)Group section
[abc]In range (a, b or c)
[^abc]Not in range
\sWhite space
a?Zero or one of a
a*Zero or more of a
a*?Zero or more, ungreedy
a+One or more of a
a+?One or more, ungreedy
a{3}Exactly 3 of a
a{3,}3 or more of a
a{,6}Up to 6 of a
a{3,6}3 to 6 of a
a{3,6}?3 to 6 of a, ungreedy
\Escape character
[:punct:]Any punctu­ation symbol
[:space:]Any space character
[:blank:]Space or tab

##Pattern Modifiers

characterdescription
gGlobal match
i *Case-i­nse­nsitive
m *Multiple lines
s *Treat string as single line
x *Allow comments and whitespace in pattern
e *Evaluate replac­ement
U *Ungreedy pattern

* PCRE modifier

##PHP fopen() Modes

characterdescription
rRead
r+Read and write, prepend
wWrite, truncate
w+Read and write, truncate
aWrite, append
a+Read and write, append

##PHP Date Formatting

characterdescription
Y4 digit year (2008)
y2 digit year (08)
FLong month (January)
MShort month (Jan)
mMonth ⁴ (01 to 12)
nMonth (1 to 12)
DShort day name (Mon)
lLong day name (Monday) (lowercase L)
dDay ⁴ (01 to 31)
jDay (1 to 31)
h12 Hour ⁴ (01 to 12)
g12 Hour (1 to 12)
H24 Hour ⁴ (00 to 23)
G24 Hour (0 to 23)
iMinutes ⁴ (00 to 59)
sSeconds ⁴ (00 to 59)
wDay of week ¹ (0 to 6)
zDay of year (0 to 365)
WWeek of year ² (1 to 53)
tDays in month (28 to 31)
aam or pm
AAM or PM
BSwatch Internet Time (000 to 999)
SOrdinal Suffix (st, nd, rd, th)
TTimezone of machine (GMT)
ZTimezone offset (seconds)
OGMT offset (hours) (+0200)
IDaylight saving (1 or 0)
LLeap year (1 or 0)
USeconds since Epoch ³
cISO 8601 (PHP 5) (2008-­07-­31T­18:­30:­13+­01:00)
rRFC 2822 (Thu, 31 Jul 2008 18:30:13 +0100)
  • ¹ 0 is Sunday, 6 is Saturday.
  • ² Week that overlaps two years belongs to year that contains most days of that week. Hence week number for 1st January of a given year can be 53 if week belongs to previous year. date("W­", mktime(0, 0, 0, 12, 8, $year)) always gives correct number of weeks in $year.
  • ³ The Epoch is the 1st January 1970.
  • ⁴ With leading zeroes

##Comparisons of $x with PHP functions

Expressiongettype()empty()is_null()isset()boolean : if($x)
$x = "";stringTRUEFALSETRUEFALSE
$x = null;NULLTRUETRUEFALSEFALSE
var $x;NULLTRUETRUEFALSEFALSE
$x is undefinedNULLTRUETRUEFALSEFALSE
$x = array();arrayTRUEFALSETRUEFALSE
$x = array('a', 'b');arrayFALSEFALSETRUETRUE
$x = false;booleanTRUEFALSETRUEFALSE
$x = true;booleanFALSEFALSETRUETRUE
$x = 1;integerFALSEFALSETRUETRUE
$x = 42;integerFALSEFALSETRUETRUE
$x = 0;integerTRUEFALSETRUEFALSE
$x = -1;integerFALSEFALSETRUETRUE
$x = "1";stringFALSEFALSETRUETRUE
$x = "0";stringTRUEFALSETRUEFALSE
$x = "-1";stringFALSEFALSETRUETRUE
$x = "php";stringFALSEFALSETRUETRUE
$x = "true";stringFALSEFALSETRUETRUE
$x = "false";stringFALSEFALSETRUETRUE

##Loose comparisons with == |TRUE|FALSE|1|0|-1|"1"|"0"|"-1"|NULL|array()|"php"|"" -----|-----|-----|-----|-----|-----|-----|-----|-----|-----|-----|-----|----- TRUE|TRUE|FALSE|TRUE|FALSE|TRUE|TRUE|FALSE|TRUE|FALSE|FALSE|TRUE|FALSE FALSE|FALSE|TRUE|FALSE|TRUE|FALSE|FALSE|TRUE|FALSE|TRUE|TRUE|FALSE|TRUE 1|TRUE|FALSE|TRUE|FALSE|FALSE|TRUE|FALSE|FALSE|FALSE|FALSE|FALSE|FALSE 0|FALSE|TRUE|FALSE|TRUE|FALSE|FALSE|TRUE|FALSE|TRUE|FALSE|TRUE|TRUE -1|TRUE|FALSE|FALSE|FALSE|TRUE|FALSE|FALSE|TRUE|FALSE|FALSE|FALSE|FALSE "1"|TRUE|FALSE|TRUE|FALSE|FALSE|TRUE|FALSE|FALSE|FALSE|FALSE|FALSE|FALSE "0"|FALSE|TRUE|FALSE|TRUE|FALSE|FALSE|TRUE|FALSE|FALSE|FALSE|FALSE|FALSE "-1"|TRUE|FALSE|FALSE|FALSE|TRUE|FALSE|FALSE|TRUE|FALSE|FALSE|FALSE|FALSE NULL|FALSE|TRUE|FALSE|TRUE|FALSE|FALSE|FALSE|FALSE|TRUE|TRUE|FALSE|TRUE array()|FALSE|TRUE|FALSE|FALSE|FALSE|FALSE|FALSE|FALSE|TRUE|TRUE|FALSE|FALSE "php"|TRUE|FALSE|FALSE|TRUE|FALSE|FALSE|FALSE|FALSE|FALSE|FALSE|TRUE|FALSE ""|FALSE|TRUE|FALSE|TRUE|FALSE|FALSE|FALSE|FALSE|TRUE|FALSE|FALSE|TRUE

##Strict comparisons with === |TRUE|FALSE|1|0|-1|"1"|"0"|"-1"|NULL|array()|"php"|"" -----|-----|-----|-----|-----|-----|-----|-----|-----|-----|-----|-----|----- TRUE|TRUE|FALSE|FALSE|FALSE|FALSE|FALSE|FALSE|FALSE|FALSE|FALSE|FALSE|FALSE FALSE|FALSE|TRUE|FALSE|FALSE|FALSE|FALSE|FALSE|FALSE|FALSE|FALSE|FALSE|FALSE 1|FALSE|FALSE|TRUE|FALSE|FALSE|FALSE|FALSE|FALSE|FALSE|FALSE|FALSE|FALSE 0|FALSE|FALSE|FALSE|TRUE|FALSE|FALSE|FALSE|FALSE|FALSE|FALSE|FALSE|FALSE -1|FALSE|FALSE|FALSE|FALSE|TRUE|FALSE|FALSE|FALSE|FALSE|FALSE|FALSE|FALSE "1"|FALSE|FALSE|FALSE|FALSE|FALSE|TRUE|FALSE|FALSE|FALSE|FALSE|FALSE|FALSE "0"|FALSE|FALSE|FALSE|FALSE|FALSE|FALSE|TRUE|FALSE|FALSE|FALSE|FALSE|FALSE "-1"|FALSE|FALSE|FALSE|FALSE|FALSE|FALSE|FALSE|TRUE|FALSE|FALSE|FALSE|FALSE NULL|FALSE|FALSE|FALSE|FALSE|FALSE|FALSE|FALSE|FALSE|TRUE|FALSE|FALSE|FALSE array()|FALSE|FALSE|FALSE|FALSE|FALSE|FALSE|FALSE|FALSE|FALSE|TRUE|FALSE|FALSE "php"|FALSE|FALSE|FALSE|FALSE|FALSE|FALSE|FALSE|FALSE|FALSE|FALSE|TRUE|FALSE ""|FALSE|FALSE|FALSE|FALSE|FALSE|FALSE|FALSE|FALSE|FALSE|FALSE|FALSE|TRUE

##Comparison Operators

ExampleNameResult
$a == $bEqualTRUE if $a is equal to $b after type juggling.
$a === $bIdenticalTRUE if $a is equal to $b, and they are of the same type.
$a != $bNot equalTRUE if $a is not equal to $b after type juggling.
$a <> $bNot equalTRUE if $a is not equal to $b after type juggling.
$a !== $bNot identicalTRUE if $a is not equal to $b, or they are not of the same type.
$a < $bLess thanTRUE if $a is strictly less than $b.
$a > $bGreater thanTRUE if $a is strictly greater than $b.
$a <= $bLess than or equal toTRUE if $a is less than or equal to $b.
$a >= $bGreater than or equal toTRUE if $a is greater than or equal to $b.
$a <=> $bSpaceshipAn integer less than, equal to, or greater than zero when $a is respectively less than, equal to, or greater than $b. Available as of PHP 7.
$a ?? $b ?? $cNull coalescingThe first operand from left to right that exists and is not NULL. NULL if no values are defined and not NULL. Available as of PHP 7.