\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{Beyk (nim)} \pdfinfo{ /Title (php-for-ever.pdf) /Creator (Cheatography) /Author (Beyk (nim)) /Subject (Php for ever 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}{A3A3A3} \definecolor{LightBackground}{HTML}{F3F3F3} \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 for ever Cheat Sheet}}}} \\ \normalsize{by \textcolor{DarkBackground}{Beyk (nim)} via \textcolor{DarkBackground}{\uline{cheatography.com/32591/cs/10057/}}} \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}Beyk (nim) \\ \uline{cheatography.com/nim} \\ \end{tabulary} \vfill \columnbreak \begin{tabulary}{5.8cm}{L} \SetRowColor{FootBackground} \mymulticolumn{1}{p{5.377cm}}{\bf\textcolor{white}{Cheat Sheet}} \\ \vspace{-2pt}Not Yet Published.\\ Updated 4th December, 2016.\\ 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*}{3} \begin{tabularx}{5.377cm}{x{1.89126 cm} x{3.08574 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{5.377cm}}{\bf\textcolor{white}{About Php}} \tn % Row 0 \SetRowColor{LightBackground} Before you continue: & you should have a basic understanding of the following: {\bf{ HTML CSS JavaScript}} \tn % Row Count 4 (+ 4) % Row 1 \SetRowColor{white} PHP is an acronym for & "PHP: {\bf{Hypertext Preprocessor}}" \tn % Row Count 6 (+ 2) % Row 2 \SetRowColor{LightBackground} {\bf{PHP is}} & a widely-used, open source scripting language \tn % Row Count 8 (+ 2) % Row 3 \SetRowColor{white} {\bf{PHP is}} & free to download and use: www.php.net \tn % Row Count 10 (+ 2) % Row 4 \SetRowColor{LightBackground} {\bf{Php}} & PHP is a server scripting language \tn % Row Count 12 (+ 2) % Row 5 \SetRowColor{white} PHP files have extension & ".php" \tn % Row Count 14 (+ 2) % Row 6 \SetRowColor{LightBackground} PHP files can contain & text, HTML, CSS, JavaScript, and PHP code \tn % Row Count 16 (+ 2) \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{x{2.4885 cm} x{2.4885 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{5.377cm}}{\bf\textcolor{white}{Basic PHP Syntax}} \tn % Row 0 \SetRowColor{LightBackground} A PHP script starts with \textless{}?php and ends with ?\textgreater{}: & \textless{}?php // PHP code goes here ?\textgreater{} \tn % Row Count 3 (+ 3) % Row 1 \SetRowColor{white} A PHP file normally contains HTML tags, and some PHP scripting code. & ` \textless{}!DOCTYPE html\textgreater{} \textless{}html\textgreater{} \textless{}body\textgreater{} \textless{}h1\textgreater{}My first PHP page\textless{}/h1\textgreater{} \textless{}?php echo "Hello World!"; ?\textgreater{} \textless{}/body\textgreater{} \textless{}/html\textgreater{}` \tn % Row Count 9 (+ 6) % Row 2 \SetRowColor{LightBackground} Comments in PHP & // single-line comment \tn % Row Count 11 (+ 2) \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{x{2.4885 cm} x{2.4885 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{5.377cm}}{\bf\textcolor{white}{Basic PHP Syntax}} \tn % Row 0 \SetRowColor{LightBackground} A PHP script starts with \textless{}?php and ends with ?\textgreater{}: & \textless{}?php // PHP code goes here ?\textgreater{} \tn % Row Count 3 (+ 3) % Row 1 \SetRowColor{white} A PHP file normally contains HTML tags, and some PHP scripting code. & ` \textless{}!DOCTYPE html\textgreater{} \textless{}html\textgreater{} \textless{}body\textgreater{} \textless{}h1\textgreater{}My first PHP page\textless{}/h1\textgreater{} \textless{}?php echo "Hello World!"; ?\textgreater{} \textless{}/body\textgreater{} \textless{}/html\textgreater{}` \tn % Row Count 9 (+ 6) % Row 2 \SetRowColor{LightBackground} Comments in PHP & // or \# are the single-line comment And /* multiple-lines comment block */ \tn % Row Count 13 (+ 4) % Row 3 \SetRowColor{white} PHP Case Sensitivity: & In PHP, all keywords (e.g. if, else, while, echo, etc.), classes, functions, and user-defined functions are NOT case-sensitive.{\bf{However; all variable names are case-sensitive.}} \tn % Row Count 22 (+ 9) % Row 4 \SetRowColor{LightBackground} Creating (Declaring) PHP Variables & variable starts with the \$ sign, \$x = 5; \tn % Row Count 24 (+ 2) % Row 5 \SetRowColor{white} Output Variables & echo statement: echo \$x \tn % Row Count 26 (+ 2) % Row 6 \SetRowColor{LightBackground} PHP is a Loosely Typed Language & we did not have to tell PHP which data type the variable is. PHP automatically converts the variable to the correct data type, depending on its value. \tn % Row Count 34 (+ 8) \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{5.377cm}}{\bf\textcolor{white}{The Point 1}} \tn \SetRowColor{white} \mymulticolumn{1}{x{5.377cm}}{PHP The static Keyword \newline % Row Count 1 (+ 1) Normally, when a function is completed/executed, all of its variables are deleted. However, sometimes we want a local variable NOT to be deleted. We need it for a further job. \newline % Row Count 5 (+ 4) To do this, use the static keyword when you first declare the variable: static \$x = 0;% Row Count 7 (+ 2) } \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{x{1.29402 cm} x{3.68298 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{5.377cm}}{\bf\textcolor{white}{PHP 5 Data Types}} \tn % Row 0 \SetRowColor{LightBackground} PHP Data Types & String Integer Float (floating point numbers - also called double) Boolean Array Object NULL Resource \tn % Row Count 5 (+ 5) % Row 1 \SetRowColor{white} PHP Array & An array stores multiple values in one single variable.\$cars = array("Volvo","BMW","Toyota"); \tn % Row Count 9 (+ 4) % Row 2 \SetRowColor{LightBackground} PHP Object & is a data type which stores data and information on how to process that data. \tn % Row Count 12 (+ 3) % Row 3 \SetRowColor{white} PHP NULL Value & Null is a special data type which can have only one value: NULL.\$x = null; \tn % Row Count 15 (+ 3) % Row 4 \SetRowColor{LightBackground} PHP Resource & A common example of using the resource data type is a database call. \tn % Row Count 18 (+ 3) \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{5.377cm}}{\bf\textcolor{white}{PHP 5 Constants}} \tn \SetRowColor{white} \mymulticolumn{1}{x{5.377cm}}{A constant is an identifier (name) for a simple value. The value cannot be changed during the script. \newline % Row Count 3 (+ 3) To create a constant, use the define() function. \newline % Row Count 4 (+ 1) Syntax \newline % Row Count 5 (+ 1) define(name, value, case-insensitive) \newline % Row Count 6 (+ 1) \textless{}?php \newline % Row Count 7 (+ 1) define("GREETING", "Welcome to Amozeh.com!"); \newline % Row Count 8 (+ 1) echo GREETING; \newline % Row Count 9 (+ 1) ?\textgreater{}% Row Count 10 (+ 1) } \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{x{2.04057 cm} x{2.93643 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{5.377cm}}{\bf\textcolor{white}{PHP 5 Operators}} \tn % Row 0 \SetRowColor{LightBackground} === Identical & \$x === \$y Returns true if \$x is equal to \$y, and they are of the same type \tn % Row Count 4 (+ 4) % Row 1 \SetRowColor{white} !== Not identical & Returns true if \$x is not equal to \$y, or they are not of the same type \tn % Row Count 8 (+ 4) % Row 2 \SetRowColor{LightBackground} PHP String Operators & PHP has two operators that are specially designed for strings. \tn % Row Count 11 (+ 3) % Row 3 \SetRowColor{white} . Concatenation & \$txt1 . \$txt2 Concatenation of \$txt1 and \$txt2 \tn % Row Count 14 (+ 3) % Row 4 \SetRowColor{LightBackground} .= Concatenation assignment & \$txt1 .= \$txt2 Appends \$txt2 to \$txt1 \tn % Row Count 16 (+ 2) \hhline{>{\arrayrulecolor{DarkBackground}}--} \SetRowColor{LightBackground} \mymulticolumn{2}{x{5.377cm}}{PHP divides the operators in the following groups: \newline \newline Arithmetic operators \newline Assignment operators \newline Comparison operators \newline Increment/Decrement operators \newline Logical operators \newline String operators \newline Array operators} \tn \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{x{1.89126 cm} x{3.08574 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{5.377cm}}{\bf\textcolor{white}{PHP Conditional Statements}} \tn % Row 0 \SetRowColor{LightBackground} The if Statement & `if (\$t \textless{} "20") \{ echo "Have a good day!"; ` \tn % Row Count 2 (+ 2) % Row 1 \SetRowColor{white} The if...else Statement & `if (\$t \textless{} "20") \{ echo "Have a good day!"; \} else \{ echo "Have a good night!"; \} ?\textgreater{}` \tn % Row Count 6 (+ 4) % Row 2 \SetRowColor{LightBackground} The PHP switch Statement & `switch (\$favcolor) \{ case "red": echo "Your favorite color is red!"; break; ... default: echo "Your favorite color isn't exists!!"; \}` \tn % Row Count 14 (+ 8) \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{5.377cm}}{\bf\textcolor{white}{PHP Loops}} \tn \SetRowColor{white} \mymulticolumn{1}{x{5.377cm}}{while - loops through a block of code as long as the specified condition is true \newline % Row Count 2 (+ 2) do...while - loops through a block of code once, and then repeats the loop as long as the specified condition is true \newline % Row Count 5 (+ 3) for - loops through a block of code a specified number of times \newline % Row Count 7 (+ 2) foreach - loops through a block of code for each element in an array \newline % Row Count 9 (+ 2) `\textless{}?php \newline % Row Count 10 (+ 1) \$colors = array("red", "green", "blue", "yellow"); \newline % Row Count 12 (+ 2) foreach (\$colors as \$value) \{ \newline % Row Count 13 (+ 1) echo "\$value \textless{}br\textgreater{}"; \newline % Row Count 14 (+ 1) \} \newline % Row Count 15 (+ 1) ?\textgreater{} `% Row Count 16 (+ 1) } \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{5.377cm}}{\bf\textcolor{white}{PHP 5 Functions}} \tn \SetRowColor{white} \mymulticolumn{1}{x{5.377cm}}{The real power of PHP comes from its functions; it has more than 1000 built-in functions. \newline % Row Count 2 (+ 2) Besides the built-in PHP functions, we can create our own functions. \newline % Row Count 4 (+ 2) \textless{}?php \newline % Row Count 5 (+ 1) function sum(\$x, \$y) \{ \newline % Row Count 6 (+ 1) \$z = \$x + \$y; \newline % Row Count 7 (+ 1) return \$z; \newline % Row Count 8 (+ 1) \} \newline % Row Count 9 (+ 1) echo "5 + 10 = " . sum(5, 10) . "\textless{}br\textgreater{}"; \newline % Row Count 10 (+ 1) echo "7 + 13 = " . sum(7, 13) . "\textless{}br\textgreater{}"; \newline % Row Count 11 (+ 1) echo "2 + 4 = " . sum(2, 4); \newline % Row Count 12 (+ 1) ?\textgreater{}% Row Count 13 (+ 1) } \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{x{2.4885 cm} x{2.4885 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{5.377cm}}{\bf\textcolor{white}{PHP 5 Arrays}} \tn % Row 0 \SetRowColor{LightBackground} An array stores multiple values in one single variable: & \$cars = array("Volvo", "BMW", "Toyota"); \tn % Row Count 3 (+ 3) % Row 1 \SetRowColor{white} In PHP, there are three types of arrays: & {\bf{ Indexed }}arrays - Arrays with a numeric index {\bf{Associative }}arrays - Arrays with named keys {\bf{ Multidimensional }}arrays - Arrays containing one or more arrays \tn % Row Count 12 (+ 9) % Row 2 \SetRowColor{LightBackground} Indexed Arrays & `\$cars = array("Volvo", "BMW", "Toyota");` OR `\$cars{[}0{]} = "Volvo"; \$cars{[}1{]} = "BMW"; \$cars{[}2{]} = "Toyota";` \tn % Row Count 18 (+ 6) % Row 3 \SetRowColor{white} The count() function is used to return the length (the number of elements) of an array: & ` \textless{}?php \$cars = array("Volvo", "BMW", "Toyota"); echo count(\$cars); ?\textgreater{} ` \tn % Row Count 23 (+ 5) % Row 4 \SetRowColor{LightBackground} Loop Through an Indexed Array & ` \textless{}?php \$cars = array("Volvo", "BMW", "Toyota"); \$arrlength = count(\$cars); for(\$x = 0; \$x \textless{} \$arrlength; \$x++) \{ echo \$cars{[}\$x{]}; echo "\textless{}br\textgreater{}"; \} ?\textgreater{} ` \tn % Row Count 31 (+ 8) \end{tabularx} \par\addvspace{1.3em} \vfill \columnbreak \begin{tabularx}{5.377cm}{x{2.4885 cm} x{2.4885 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{5.377cm}}{\bf\textcolor{white}{PHP 5 Arrays (cont)}} \tn % Row 5 \SetRowColor{LightBackground} Associative Arrays & Associative arrays are arrays that use named keys that you assign to them. \tn % Row Count 4 (+ 4) % Row 6 \SetRowColor{white} There are two ways to create an associative array: & `\$age = array("Peter"=\textgreater{}"35", "Ben"=\textgreater{}"37", "Joe"=\textgreater{}"43");` OR ` \$age{[}'Peter'{]} = "35"; \$age{[}'Ben'{]} = "37"; \$age{[}'Joe'{]} = "43"; ` \tn % Row Count 11 (+ 7) % Row 7 \SetRowColor{LightBackground} Loop Through an Associative Array & ` \textless{}?php \$age = array("Peter"=\textgreater{}"35", "Ben"=\textgreater{}"37", "Joe"=\textgreater{}"43"); foreach(\$age as \$x =\textgreater{} \$x\_value) \{ echo "Key=" . \$x . ", Value=" . \$x\_value; echo "\textless{}br\textgreater{}"; \} ?\textgreater{} ` \tn % Row Count 20 (+ 9) \hhline{>{\arrayrulecolor{DarkBackground}}--} \SetRowColor{LightBackground} \mymulticolumn{2}{x{5.377cm}}{Multidimensional arrays will be explained in the PHP advanced section.} \tn \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} % That's all folks \end{multicols*} \end{document}