Skip to content

Instantly share code, notes, and snippets.

@mkormendy
Created January 21, 2016 05:26
Show Gist options
  • Save mkormendy/d65aac90d6907f62c0e0 to your computer and use it in GitHub Desktop.
Save mkormendy/d65aac90d6907f62c0e0 to your computer and use it in GitHub Desktop.
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

characters description
^ 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
\s White 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

character description
g Global 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

character description
r Read
r+ Read and write, prepend
w Write, truncate
w+ Read and write, truncate
a Write, append
a+ Read and write, append

##PHP Date Formatting

character description
Y 4 digit year (2008)
y 2 digit year (08)
F Long month (January)
M Short month (Jan)
m Month ⁴ (01 to 12)
n Month (1 to 12)
D Short day name (Mon)
l Long day name (Monday) (lowercase L)
d Day ⁴ (01 to 31)
j Day (1 to 31)

| h|12 Hour ⁴ (01 to 12) g|12 Hour (1 to 12) H|24 Hour ⁴ (00 to 23) G|24 Hour (0 to 23) i|Minutes ⁴ (00 to 59) s|Seconds ⁴ (00 to 59) | w|Day of week ¹ (0 to 6) z|Day of year (0 to 365) W|Week of year ² (1 to 53) t|Days in month (28 to 31) | a|am or pm A|AM or PM B|Swatch Internet Time (000 to 999) S|Ordinal Suffix (st, nd, rd, th) | T|Timezone of machine (GMT) Z|Timezone offset (seconds) O|GMT offset (hours) (+0200) I|Daylight saving (1 or 0) L|Leap year (1 or 0) | U|Seconds since Epoch ³ c|ISO 8601 (PHP 5) (2008-­07-­31T­18:­30:­13+­01:00) r|RFC 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

Expression gettype() empty() is_null() isset() boolean : if($x)
$x = ""; string TRUE FALSE TRUE FALSE
$x = null; NULL TRUE TRUE FALSE FALSE
var $x; NULL TRUE TRUE FALSE FALSE
$x is undefined NULL TRUE TRUE FALSE FALSE
$x = array(); array TRUE FALSE TRUE FALSE
$x = array('a', 'b'); array FALSE FALSE TRUE TRUE
$x = false; boolean TRUE FALSE TRUE FALSE
$x = true; boolean FALSE FALSE TRUE TRUE
$x = 1; integer FALSE FALSE TRUE TRUE
$x = 42; integer FALSE FALSE TRUE TRUE
$x = 0; integer TRUE FALSE TRUE FALSE
$x = -1; integer FALSE FALSE TRUE TRUE
$x = "1"; string FALSE FALSE TRUE TRUE
$x = "0"; string TRUE FALSE TRUE FALSE
$x = "-1"; string FALSE FALSE TRUE TRUE
$x = "php"; string FALSE FALSE TRUE TRUE
$x = "true"; string FALSE FALSE TRUE TRUE
$x = "false"; string FALSE FALSE TRUE TRUE

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

Example Name Result
$a == $b Equal TRUE if $a is equal to $b after type juggling.
$a === $b Identical TRUE if $a is equal to $b, and they are of the same type.
$a != $b Not equal TRUE if $a is not equal to $b after type juggling.
$a <> $b Not equal TRUE if $a is not equal to $b after type juggling.
$a !== $b Not identical TRUE if $a is not equal to $b, or they are not of the same type.
$a < $b Less than TRUE if $a is strictly less than $b.
$a > $b Greater than TRUE if $a is strictly greater than $b.
$a <= $b Less than or equal to TRUE if $a is less than or equal to $b.
$a >= $b Greater than or equal to TRUE if $a is greater than or equal to $b.
$a <=> $b Spaceship An 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 ?? $c Null coalescing The 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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment