Show Menu

PHP Cheat Sheet by DaveChild

A quick reference guide for PHP, with functions references, a regular expression syntax guide and a reference for PHP's date formating functions. As of 28/6/14, the cheat sheet now includes popup links to the appropriate PHP manual pages.

 

Regular Expres­sions Syntax

^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

There's an excellent regular expression tester at: http:/­/re­gex­pal.com/

Pattern Modifiers

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

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

PHP Date Formatting

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

 

Share This Cheat Sheet!

Favourited by 54 Members:

guilhermecruz chi-zach SkyeBella McCloud BobGuzek dacheat ThibaultGomarin Yennick CenturyMan1979 mrlaziz gmickd and 44 more ...

Comments

localwebdesign localwebdesign, 10:02 28 Nov 11

Dave,

Thanks for adding these. They rock! the mysql and php ones are my faves. Found your site by searching for a SQL Server cheatsheet.

all of them are great!

Now to dream up one of my own to share. ;0)

Anan Anan, 12:17 7 Dec 11

Hi Dave!
I recently started learning html,php,mysql and I found your cheatsheets very helpful and also the four security lessons on Php scripting(addedbytes).

thanks alot,
with bllesings from aboce

Anan

alan alan, 07:46 5 Jan 12

this is better, got some ajax accordion function for clicking

Fishtankalpha Fishtankalpha, 13:11 5 Jan 12

There is a lot missing.

DaveChild DaveChild, 13:12 5 Jan 12

Well, yes. It's a quick reference, not a manual :).

xcession xcession, 18:23 14 Mar 12

Nice Thanks :)
btw, can you make it a bit more compact? because table break into separated page when download as PDF.

Isaac Isaac, 13:28 14 May 12

What good is this cheat sheet? It has function names and no descriptions or examples?

Is this a wordpress website? WTF!

Add a Comment

Your Comment

Please enter your name.

    Please enter your email address

      Please enter your Comment.