\documentclass[10pt,a4paper]{article} % Packages \usepackage{fancyhdr} % For header and footer \usepackage{multicol} % Allows multicols in tables \usepackage{tabularx} % Intelligent column widths \usepackage{tabulary} % Used in header and footer \usepackage{hhline} % Border under tables \usepackage{graphicx} % For images \usepackage{xcolor} % For hex colours %\usepackage[utf8x]{inputenc} % For unicode character support \usepackage[T1]{fontenc} % Without this we get weird character replacements \usepackage{colortbl} % For coloured tables \usepackage{setspace} % For line height \usepackage{lastpage} % Needed for total page number \usepackage{seqsplit} % Splits long words. %\usepackage{opensans} % Can't make this work so far. Shame. Would be lovely. \usepackage[normalem]{ulem} % For underlining links % Most of the following are not required for the majority % of cheat sheets but are needed for some symbol support. \usepackage{amsmath} % Symbols \usepackage{MnSymbol} % Symbols \usepackage{wasysym} % Symbols %\usepackage[english,german,french,spanish,italian]{babel} % Languages % Document Info \author{davidsykora} \pdfinfo{ /Title (php-essentials.pdf) /Creator (Cheatography) /Author (davidsykora) /Subject (PHP Essentials Cheat Sheet) } % Lengths and widths \addtolength{\textwidth}{6cm} \addtolength{\textheight}{-1cm} \addtolength{\hoffset}{-3cm} \addtolength{\voffset}{-2cm} \setlength{\tabcolsep}{0.2cm} % Space between columns \setlength{\headsep}{-12pt} % Reduce space between header and content \setlength{\headheight}{85pt} % If less, LaTeX automatically increases it \renewcommand{\footrulewidth}{0pt} % Remove footer line \renewcommand{\headrulewidth}{0pt} % Remove header line \renewcommand{\seqinsert}{\ifmmode\allowbreak\else\-\fi} % Hyphens in seqsplit % This two commands together give roughly % the right line height in the tables \renewcommand{\arraystretch}{1.3} \onehalfspacing % Commands \newcommand{\SetRowColor}[1]{\noalign{\gdef\RowColorName{#1}}\rowcolor{\RowColorName}} % Shortcut for row colour \newcommand{\mymulticolumn}[3]{\multicolumn{#1}{>{\columncolor{\RowColorName}}#2}{#3}} % For coloured multi-cols \newcolumntype{x}[1]{>{\raggedright}p{#1}} % New column types for ragged-right paragraph columns \newcommand{\tn}{\tabularnewline} % Required as custom column type in use % Font and Colours \definecolor{HeadBackground}{HTML}{333333} \definecolor{FootBackground}{HTML}{666666} \definecolor{TextColor}{HTML}{333333} \definecolor{DarkBackground}{HTML}{8ABDFF} \definecolor{LightBackground}{HTML}{F0F6FF} \renewcommand{\familydefault}{\sfdefault} \color{TextColor} % Header and Footer \pagestyle{fancy} \fancyhead{} % Set header to blank \fancyfoot{} % Set footer to blank \fancyhead[L]{ \noindent \begin{multicols}{3} \begin{tabulary}{5.8cm}{C} \SetRowColor{DarkBackground} \vspace{-7pt} {\parbox{\dimexpr\textwidth-2\fboxsep\relax}{\noindent \hspace*{-6pt}\includegraphics[width=5.8cm]{/web/www.cheatography.com/public/images/cheatography_logo.pdf}} } \end{tabulary} \columnbreak \begin{tabulary}{11cm}{L} \vspace{-2pt}\large{\bf{\textcolor{DarkBackground}{\textrm{PHP Essentials Cheat Sheet}}}} \\ \normalsize{by \textcolor{DarkBackground}{davidsykora} via \textcolor{DarkBackground}{\uline{cheatography.com/43180/cs/12892/}}} \end{tabulary} \end{multicols}} \fancyfoot[L]{ \footnotesize \noindent \begin{multicols}{3} \begin{tabulary}{5.8cm}{LL} \SetRowColor{FootBackground} \mymulticolumn{2}{p{5.377cm}}{\bf\textcolor{white}{Cheatographer}} \\ \vspace{-2pt}davidsykora \\ \uline{cheatography.com/davidsykora} \\ \end{tabulary} \vfill \columnbreak \begin{tabulary}{5.8cm}{L} \SetRowColor{FootBackground} \mymulticolumn{1}{p{5.377cm}}{\bf\textcolor{white}{Cheat Sheet}} \\ \vspace{-2pt}Published 6th October, 2017.\\ Updated 21st September, 2017.\\ Page {\thepage} of \pageref{LastPage}. \end{tabulary} \vfill \columnbreak \begin{tabulary}{5.8cm}{L} \SetRowColor{FootBackground} \mymulticolumn{1}{p{5.377cm}}{\bf\textcolor{white}{Sponsor}} \\ \SetRowColor{white} \vspace{-5pt} %\includegraphics[width=48px,height=48px]{dave.jpeg} Measure your website readability!\\ www.readability-score.com \end{tabulary} \end{multicols}} \begin{document} \raggedright \raggedcolumns % Set font size to small. Switch to any value % from this page to resize cheat sheet text: % www.emerson.emory.edu/services/latex/latex_169.html \footnotesize % Small font. \begin{multicols*}{2} \begin{tabularx}{8.4cm}{x{4.96 cm} x{3.04 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{8.4cm}}{\bf\textcolor{white}{Language basics}} \tn % Row 0 \SetRowColor{LightBackground} script start & \textless{}?php \tn % Row Count 1 (+ 1) % Row 1 \SetRowColor{white} script end & ?\textgreater{} or nothing \tn % Row Count 2 (+ 1) % Row 2 \SetRowColor{LightBackground} command termination & ; \tn % Row Count 3 (+ 1) % Row 3 \SetRowColor{white} inline comment & `// simple oneliner` \tn % Row Count 5 (+ 2) % Row 4 \SetRowColor{LightBackground} block comment & `/* multiline text */` \tn % Row Count 7 (+ 2) % Row 5 \SetRowColor{white} run script from file (if error throws warning) & \seqsplit{`include('script}.php');` \tn % Row Count 9 (+ 2) % Row 6 \SetRowColor{LightBackground} run script from file (errors are fatal) & \seqsplit{`require('script}.php');` \tn % Row Count 11 (+ 2) \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{8.4cm}{x{4.24 cm} x{3.76 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{8.4cm}}{\bf\textcolor{white}{Variables}} \tn % Row 0 \SetRowColor{LightBackground} declaration without value & `\$variableName;` \tn % Row Count 2 (+ 2) % Row 1 \SetRowColor{white} string declaration & `\$string = "some text";` \tn % Row Count 4 (+ 2) % Row 2 \SetRowColor{LightBackground} integer declaration & `\$int = 123;` \tn % Row Count 5 (+ 1) % Row 3 \SetRowColor{white} double declaration & `\$doble = 1.2` \tn % Row Count 6 (+ 1) % Row 4 \SetRowColor{LightBackground} empty array declaration & `\$array = array();` \tn % Row Count 8 (+ 2) % Row 5 \SetRowColor{white} array with elements declaration & `\$a = array("text",1,2.3);` \tn % Row Count 10 (+ 2) % Row 6 \SetRowColor{LightBackground} boolean declaration & `\$bool = true;` \tn % Row Count 11 (+ 1) % Row 7 \SetRowColor{white} null declaration & `\$empty = null;` \tn % Row Count 12 (+ 1) \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{8.4cm}{x{3.92 cm} x{4.08 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{8.4cm}}{\bf\textcolor{white}{Variable operations}} \tn % Row 0 \SetRowColor{LightBackground} addition & `\$int1 + \$int2` -\textgreater{} 12 \tn % Row Count 2 (+ 2) % Row 1 \SetRowColor{white} divison & `6 / 3` -\textgreater{} 2 \tn % Row Count 3 (+ 1) % Row 2 \SetRowColor{LightBackground} multiplication & `6 * 6` -\textgreater{} 36 \tn % Row Count 4 (+ 1) % Row 3 \SetRowColor{white} exponentiation & `2 ** 4` -\textgreater{} 16 \tn % Row Count 5 (+ 1) % Row 4 \SetRowColor{LightBackground} substraction & `2 - 1` -\textgreater{} 1 \tn % Row Count 6 (+ 1) % Row 5 \SetRowColor{white} string + int (string get converted) \#1 & `"13shits" + 13` -\textgreater{} 26 \tn % Row Count 8 (+ 2) % Row 6 \SetRowColor{LightBackground} string + int (string get converted) \#1 & `"shitsof13" + 13` -\textgreater{} 13 \tn % Row Count 10 (+ 2) % Row 7 \SetRowColor{white} string + string & `"2dogs" + "5dogs"` -\textgreater{} 7 \tn % Row Count 12 (+ 2) % Row 8 \SetRowColor{LightBackground} strings {\bf{concat}} & `\$str1 . \$str2` -\textgreater{} hello stranger! \tn % Row Count 14 (+ 2) % Row 9 \SetRowColor{white} string and int {\bf{concat}} & `"numero uno - " . 1;` -\textgreater{} numero uno - 1 \tn % Row Count 16 (+ 2) % Row 10 \SetRowColor{LightBackground} {\bf{unset}} - delete variable & `unset(\$variable);` \tn % Row Count 18 (+ 2) \hhline{>{\arrayrulecolor{DarkBackground}}--} \SetRowColor{LightBackground} \mymulticolumn{2}{x{8.4cm}}{`\$int1 = 5;` \newline `\$int2 = 7;` \newline `\$str1 = "hello ";` \newline `\$str2 = " stranger!";`} \tn \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{8.4cm}{x{4 cm} x{4 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{8.4cm}}{\bf\textcolor{white}{Flow control}} \tn % Row 0 \SetRowColor{LightBackground} simple condition & `if (true)\{\{\{nl\}\}echo("condition pass");\{\{nl\}\}\}` \tn % Row Count 3 (+ 3) % Row 1 \SetRowColor{white} is var1 equal to var2? & `if (\$var1 == \$var2)\{\{\{nl\}\}echo("it is.");\{\{nl\}\}\} else \{\{\{nl\}\}echo("it ins't.")\{\{nl\}\}\}` \tn % Row Count 8 (+ 5) % Row 2 \SetRowColor{LightBackground} two vars comperation & `(\$var1 == \$var2)` \tn % Row Count 9 (+ 1) % Row 3 \SetRowColor{white} is var equal to int? & `(\$var1 == 4)` \tn % Row Count 10 (+ 1) % Row 4 \SetRowColor{LightBackground} is var lower than int? & `(\$int \textless{} 4)` \tn % Row Count 12 (+ 2) % Row 5 \SetRowColor{white} is var bigger than int? & `(\$int \textgreater{} 4)` \tn % Row Count 14 (+ 2) % Row 6 \SetRowColor{LightBackground} is var lower than 5 and not equal to zero & `(\$int \textless{} 5 \&\& \$int != 0)` \tn % Row Count 17 (+ 3) % Row 7 \SetRowColor{white} is string equal to string? & `(\$str == "dangerous")` \tn % Row Count 19 (+ 2) % Row 8 \SetRowColor{LightBackground} is num 1 or 4? & `(\$num == 1 || \$num == 4)` \tn % Row Count 21 (+ 2) \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{8.4cm}{x{4 cm} x{4 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{8.4cm}}{\bf\textcolor{white}{Array operations}} \tn % Row 0 \SetRowColor{LightBackground} {\bf{get}} array element by index & `\$array{[}index{]}` \tn % Row Count 2 (+ 2) % Row 1 \SetRowColor{white} {\bf{push}} data to {\bf{end}} of array & \seqsplit{`array\_push(\$array}, \$data);` OR `\$array{[}{]} = \$data;` \tn % Row Count 5 (+ 3) % Row 2 \SetRowColor{LightBackground} {\bf{push}} data to {\bf{specific}} index of array & `\$array{[}'myColor'{]} = "\#FFF";` \tn % Row Count 8 (+ 3) % Row 3 \SetRowColor{white} {\bf{remove \& get last}} element of array & \seqsplit{`array\_pop(\$array);`} \tn % Row Count 10 (+ 2) % Row 4 \SetRowColor{LightBackground} get {\bf{count}} of elements in array & `count(\$array);` \tn % Row Count 12 (+ 2) % Row 5 \SetRowColor{white} is specific value in array? & `in\_array(\$needle, \$array);` \tn % Row Count 14 (+ 2) \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{8.4cm}{x{4 cm} x{4 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{8.4cm}}{\bf\textcolor{white}{Output}} \tn % Row 0 \SetRowColor{LightBackground} simple print & `echo(\$variable . " and some text");` \tn % Row Count 2 (+ 2) % Row 1 \SetRowColor{white} simple print \#2 & `echo 1;` \tn % Row Count 3 (+ 1) % Row 2 \SetRowColor{LightBackground} simple print \#3 (must be without parentheses) & `echo 1,2;` -\textgreater{} 12 \tn % Row Count 6 (+ 3) % Row 3 \SetRowColor{white} print multiple lines & `echo("line1\textbackslash{}nline2\textbackslash{}n");` \tn % Row Count 8 (+ 2) % Row 4 \SetRowColor{LightBackground} print variable type and data & `print\_r(\$var);` \tn % Row Count 10 (+ 2) % Row 5 \SetRowColor{white} print variable type and data (detailed) & `var\_dump(\$var);` \tn % Row Count 12 (+ 2) % Row 6 \SetRowColor{LightBackground} conditional printing & `echo(\$var1 == 1 ? "its 1!" : "its not 1!");` \tn % Row Count 15 (+ 3) \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{8.4cm}{x{4 cm} x{4 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{8.4cm}}{\bf\textcolor{white}{Loops}} \tn % Row 0 \SetRowColor{LightBackground} {\bf{for}} - classic loop (start instruction; condition on start of each round;end instruction of each round ) & `for (\$i = 1; \$i \textless{}= 10; \$i++) \{\{\{nl\}\}echo \$i;\{\{nl\}\}\}` \tn % Row Count 6 (+ 6) % Row 1 \SetRowColor{white} {\bf{foreach}} - do something for each element in array & `foreach(\$array as \$arrayElement)\{\{\{nl\}\}var\_dump(\$arrayElement;)\{\{nl\}\}\}` \tn % Row Count 10 (+ 4) % Row 2 \SetRowColor{LightBackground} {\bf{while}} - do something if cond is true again and again if is still true & `while(true)\{\{\{nl\}\}echo("infinity!");\{\{nl\}\}\}` \tn % Row Count 14 (+ 4) % Row 3 \SetRowColor{white} {\bf{break}} - escape from loop & `break;` \tn % Row Count 16 (+ 2) % Row 4 \SetRowColor{LightBackground} {\bf{continue}} - skip current round & `continue;` \tn % Row Count 18 (+ 2) \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{8.4cm}{x{3.6 cm} x{4.4 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{8.4cm}}{\bf\textcolor{white}{PHP Filesystem Functions}} \tn % Row 0 \SetRowColor{LightBackground} check if file exists & \seqsplit{`file\_exists("/root/file}.txt");` \tn % Row Count 2 (+ 2) % Row 1 \SetRowColor{white} open a file for reading only & `\$file = fopen("./../file.txt", "r");` \tn % Row Count 4 (+ 2) % Row 2 \SetRowColor{LightBackground} get size of file in bytes & \seqsplit{`filesize("file.txt");`} \tn % Row Count 6 (+ 2) % Row 3 \SetRowColor{white} print all bytes from fopened file & `echo fread(\$file, \seqsplit{filesize("file.txt"));`} \tn % Row Count 8 (+ 2) % Row 4 \SetRowColor{LightBackground} open a file for reading only & `\$file = fopen("./../file.txt", "w");` \tn % Row Count 10 (+ 2) % Row 5 \SetRowColor{white} write to fopened file & `fwrite(\$file, "some text\textbackslash{}n");` \tn % Row Count 12 (+ 2) % Row 6 \SetRowColor{LightBackground} close file after all operations & `fclose(\$file);` \tn % Row Count 14 (+ 2) % Row 7 \SetRowColor{white} oneline file write & \seqsplit{`file\_put\_contents(\$filename}, \$data);` \tn % Row Count 16 (+ 2) % Row 8 \SetRowColor{LightBackground} oneline file read & `\$fileContent = \seqsplit{file\_get\_contents(\$fileName);`} \tn % Row Count 19 (+ 3) % Row 9 \SetRowColor{white} fopen() modes (second argument) & {\bf{r}} = Read\{\{nl\}\}{\bf{r+}} = Read,write and prepend\{\{nl\}\}{\bf{w}} = Write, truncate\{\{nl\}\}{\bf{w+}} = Read and write, truncate a Write, append\{\{nl\}\}{\bf{a+}} = Read and write, append \tn % Row Count 27 (+ 8) \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{8.4cm}{x{4 cm} x{4 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{8.4cm}}{\bf\textcolor{white}{Useful variables}} \tn % Row 0 \SetRowColor{LightBackground} \$\_SERVER & Server and execution environment information \tn % Row Count 3 (+ 3) % Row 1 \SetRowColor{white} \$\_SERVER{[}'SERVER\_ADDR'{]} & Server IP \tn % Row Count 5 (+ 2) % Row 2 \SetRowColor{LightBackground} \$\_SERVER{[}'REQUEST\_METHOD'{]} & Which request method was used to access the page; i.e. 'GET', 'HEAD', 'POST', 'PUT' \tn % Row Count 10 (+ 5) % Row 3 \SetRowColor{white} \$\_SERVER{[}'HTTP\_USER\_AGENT'{]} & User-Agent: header from the current request \tn % Row Count 13 (+ 3) % Row 4 \SetRowColor{LightBackground} \$\_SERVER{[}'REMOTE\_ADDR'{]} & The IP address from which the user is viewing the current page \tn % Row Count 17 (+ 4) % Row 5 \SetRowColor{white} \_\_FILE\_\_ & The full path and filename of the current script file \tn % Row Count 20 (+ 3) % Row 6 \SetRowColor{LightBackground} \_\_DIR\_\_ & The directory of the file \tn % Row Count 22 (+ 2) % Row 7 \SetRowColor{white} \_\_LINE\_\_ & Current line number in script file \tn % Row Count 24 (+ 2) % Row 8 \SetRowColor{LightBackground} \$\_GET & Associative array of variables passed to the current script via the URL parameters \tn % Row Count 29 (+ 5) % Row 9 \SetRowColor{white} \$\_POST & An associative array of variables passed to the current script via the HTTP POST method when using \seqsplit{application/x-www-form-urlencoded} or multipart/form-data as the HTTP Content-Type in the request \tn % Row Count 39 (+ 10) \end{tabularx} \par\addvspace{1.3em} \vfill \columnbreak \begin{tabularx}{8.4cm}{x{4 cm} x{4 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{8.4cm}}{\bf\textcolor{white}{Useful variables (cont)}} \tn % Row 10 \SetRowColor{LightBackground} \$\_FILES & An associative array of items uploaded to the current script via the HTTP POST method \tn % Row Count 5 (+ 5) \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} % That's all folks \end{multicols*} \end{document}