About ................................................................................................................................................................................... 1
Chapter 1: Getting started with PHP ................................................................................................................... 2
Section 1.1: HTML output from web server .................................................................................................................. 2
Section 1.2: Hello, World! ............................................................................................................................................... 3
Section 1.3: Non-HTML output from web server ........................................................................................................ 3
Section 1.4: PHP built-in server ..................................................................................................................................... 4
Section 1.5: PHP CLI ....................................................................................................................................................... 5
Section 1.6: Instruction Separation ............................................................................................................................... 6
Section 1.7: PHP Tags .................................................................................................................................................... 7
Chapter 2: Variables .................................................................................................................................................... 9
Section 2.1: Accessing A Variable Dynamically By Name (Variable variables) ...................................................... 9
Section 2.2: Data Types .............................................................................................................................................. 10
Section 2.3: Global variable best practices ............................................................................................................... 13
Section 2.4: Default values of uninitialized variables .............................................................................................. 14
Section 2.5: Variable Value Truthiness and Identical Operator ............................................................................. 15
Chapter 3: Variable Scope ..................................................................................................................................... 18
Section 3.1: Superglobal variables ............................................................................................................................. 18
Section 3.2: Static properties and variables ............................................................................................................. 18
Section 3.3: User-defined global variables ............................................................................................................... 19
Chapter 4: Outputting the Value of a Variable .......................................................................................... 21
Section 4.1: echo and print .......................................................................................................................................... 21
Section 4.2: Outputting a structured view of arrays and objects .......................................................................... 22
Section 4.3: String concatenation with echo ............................................................................................................. 24
Section 4.4: printf vs sprintf ........................................................................................................................................ 25
Section 4.5: Outputting large integers ...................................................................................................................... 25
Section 4.6: Output a Multidimensional Array with index and value and print into the table ............................. 26
Chapter 5: Constants ................................................................................................................................................ 28
Section 5.1: Defining constants .................................................................................................................................. 28
Section 5.2: Class Constants ...................................................................................................................................... 29
Section 5.3: Checking if constant is defined ............................................................................................................. 29
Section 5.4: Using constants ...................................................................................................................................... 31
Section 5.5: Constant arrays ...................................................................................................................................... 31
Chapter 6: Comments ............................................................................................................................................... 32
Section 6.1: Single Line Comments ............................................................................................................................ 32
Section 6.2: Multi Line Comments .............................................................................................................................. 32
Chapter 7: Types ......................................................................................................................................................... 33
Section 7.1: Type Comparison .................................................................................................................................... 33
Section 7.2: Boolean .................................................................................................................................................... 33
Section 7.3: Float ......................................................................................................................................................... 34
Section 7.4: Strings ...................................................................................................................................................... 35
Section 7.5: Callable .................................................................................................................................................... 37
Section 7.6: Resources ................................................................................................................................................ 37
Section 7.7: Type Casting ........................................................................................................................................... 38
Section 7.8: Type Juggling ......................................................................................................................................... 38
Section 7.9: Null ........................................................................................................................................................... 39
Section 7.10: Integers ................................................................................................................................................... 39
,Chapter 8: Operators ............................................................................................................................................... 41
Section 8.1: Null Coalescing Operator (??) ................................................................................................................ 41
Section 8.2: Spaceship Operator (<=>) ...................................................................................................................... 42
Section 8.3: Execution Operator (``) ........................................................................................................................... 42
Section 8.4: Incrementing (++) and Decrementing Operators (--) ........................................................................ 42
Section 8.5: Ternary Operator (?:) ............................................................................................................................. 43
Section 8.6: Logical Operators (&&/AND and ||/OR) .............................................................................................. 44
Section 8.7: String Operators (. and .=) ..................................................................................................................... 44
Section 8.8: Object and Class Operators .................................................................................................................. 44
Section 8.9: Combined Assignment (+= etc) ............................................................................................................. 46
Section 8.10: Altering operator precedence (with parentheses) ............................................................................ 46
Section 8.11: Basic Assignment (=) ............................................................................................................................. 47
Section 8.12: Association ............................................................................................................................................. 47
Section 8.13: Comparison Operators ......................................................................................................................... 47
Section 8.14: Bitwise Operators .................................................................................................................................. 49
Section 8.15: instanceof (type operator) ................................................................................................................... 51
Chapter 9: References ............................................................................................................................................. 54
Section 9.1: Assign by Reference ............................................................................................................................... 54
Section 9.2: Return by Reference .............................................................................................................................. 54
Section 9.3: Pass by Reference .................................................................................................................................. 55
Chapter 10: Arrays ...................................................................................................................................................... 58
Section 10.1: Initializing an Array ................................................................................................................................ 58
Section 10.2: Check if key exists ................................................................................................................................. 60
Section 10.3: Validating the array type ..................................................................................................................... 61
Section 10.4: Creating an array of variables ............................................................................................................ 61
Section 10.5: Checking if a value exists in array ....................................................................................................... 61
Section 10.6: ArrayAccess and Iterator Interfaces ................................................................................................... 62
Chapter 11: Array iteration ..................................................................................................................................... 66
Section 11.1: Iterating multiple arrays together ......................................................................................................... 66
Section 11.2: Using an incremental index .................................................................................................................. 67
Section 11.3: Using internal array pointers ................................................................................................................ 67
Section 11.4: Using foreach ......................................................................................................................................... 68
Section 11.5: Using ArrayObject Iterator .................................................................................................................... 70
Chapter 12: Executing Upon an Array .............................................................................................................. 71
Section 12.1: Applying a function to each element of an array .............................................................................. 71
Section 12.2: Split array into chunks .......................................................................................................................... 72
Section 12.3: Imploding an array into string ............................................................................................................. 73
Section 12.4: "Destructuring" arrays using list() ....................................................................................................... 73
Section 12.5: array_reduce ......................................................................................................................................... 73
Section 12.6: Push a Value on an Array ..................................................................................................................... 74
Chapter 13: Manipulating an Array .................................................................................................................... 76
Section 13.1: Filtering an array .................................................................................................................................... 76
Section 13.2: Removing elements from an array ..................................................................................................... 77
Section 13.3: Sorting an Array .................................................................................................................................... 78
Section 13.4: Whitelist only some array keys ............................................................................................................ 83
Section 13.5: Adding element to start of array ......................................................................................................... 83
Section 13.6: Exchange values with keys ................................................................................................................... 84
Section 13.7: Merge two arrays into one array ......................................................................................................... 84
Chapter 14: Processing Multiple Arrays Together ..................................................................................... 86
, Section 14.1: Array intersection ................................................................................................................................... 86
Section 14.2: Merge or concatenate arrays .............................................................................................................. 86
Section 14.3: Changing a multidimensional array to associative array ................................................................ 87
Section 14.4: Combining two arrays (keys from one, values from another) ........................................................ 87
Chapter 15: Datetime Class ................................................................................................................................... 89
Section 15.1: Create Immutable version of DateTime from Mutable prior PHP 5.6 .............................................. 89
Section 15.2: Add or Subtract Date Intervals ............................................................................................................ 89
Section 15.3: getTimestamp ........................................................................................................................................ 89
Section 15.4: setDate ................................................................................................................................................... 90
Section 15.5: Create DateTime from custom format ............................................................................................... 90
Section 15.6: Printing DateTimes ................................................................................................................................ 90
Chapter 16: Working with Dates and Time .................................................................................................... 92
Section 16.1: Getting the dierence between two dates / times ............................................................................. 92
Section 16.2: Convert a date into another format .................................................................................................... 92
Section 16.3: Parse English date descriptions into a Date format .......................................................................... 94
Section 16.4: Using Predefined Constants for Date Format .................................................................................... 94
Chapter 17: Loops ....................................................................................................................................................... 96
Section 17.1: continue ................................................................................................................................................... 96
Section 17.2: break ....................................................................................................................................................... 97
Section 17.3: foreach ................................................................................................................................................... 98
Section 17.4: do...while .................................................................................................................................................. 98
Section 17.5: for ............................................................................................................................................................ 99
Section 17.6: while ...................................................................................................................................................... 100
Chapter 18: Functions ............................................................................................................................................. 101
Section 18.1: Variable-length argument lists ........................................................................................................... 101
Section 18.2: Optional Parameters .......................................................................................................................... 102
Section 18.3: Passing Arguments by Reference ..................................................................................................... 103
Section 18.4: Basic Function Usage ......................................................................................................................... 104
Section 18.5: Function Scope .................................................................................................................................... 104
Chapter 19: Functional Programming ............................................................................................................ 105
Section 19.1: Closures ................................................................................................................................................. 105
Section 19.2: Assignment to variables ..................................................................................................................... 106
Section 19.3: Objects as a function .......................................................................................................................... 106
Section 19.4: Using outside variables ...................................................................................................................... 107
Section 19.5: Anonymous function ........................................................................................................................... 107
Section 19.6: Pure functions ...................................................................................................................................... 108
Section 19.7: Common functional methods in PHP ................................................................................................ 108
Section 19.8: Using built-in functions as callbacks ................................................................................................. 109
Section 19.9: Scope .................................................................................................................................................... 109
Section 19.10: Passing a callback function as a parameter .................................................................................. 109
Chapter 20: Control Structures ......................................................................................................................... 111
Section 20.1: if else .................................................................................................................................................... 111
Section 20.2: Alternative syntax for control structures ......................................................................................... 111
Section 20.3: while ..................................................................................................................................................... 111
Section 20.4: do-while ............................................................................................................................................... 112
Section 20.5: goto ..................................................................................................................................................... 112
Section 20.6: declare ................................................................................................................................................. 112
Section 20.7: include & require ................................................................................................................................ 113
Section 20.8: return ................................................................................................................................................... 114
Section 20.9: for ........................................................................................................................................................ 114
, Section 20.10: foreach ............................................................................................................................................... 115
Section 20.11: if elseif else ......................................................................................................................................... 115
Section 20.12: if .......................................................................................................................................................... 116
Section 20.13: switch .................................................................................................................................................. 116
Chapter 21: Alternative Syntax for Control Structures ......................................................................... 118
Section 21.1: Alternative if/else statement .............................................................................................................. 118
Section 21.2: Alternative for statement ................................................................................................................... 118
Section 21.3: Alternative while statement ............................................................................................................... 118
Section 21.4: Alternative foreach statement ........................................................................................................... 118
Section 21.5: Alternative switch statement ............................................................................................................. 119
Chapter 22: String Parsing ................................................................................................................................... 120
Section 22.1: Splitting a string by separators ......................................................................................................... 120
Section 22.2: Substring ............................................................................................................................................. 120
Section 22.3: Searching a substring with strpos .................................................................................................... 122
Section 22.4: Parsing string using regular expressions ......................................................................................... 123
Chapter 23: String formatting ........................................................................................................................... 124
Section 23.1: String interpolation ............................................................................................................................. 124
Section 23.2: Extracting/replacing substrings ....................................................................................................... 125
Chapter 24: Exception Handling and Error Reporting .......................................................................... 127
Section 24.1: Setting error reporting and where to display them ........................................................................ 127
Section 24.2: Logging fatal errors ........................................................................................................................... 127
Chapter 25: Sending Email ................................................................................................................................... 129
Section 25.1: Sending Email - The basics, more details, and a full example ....................................................... 129
Section 25.2: Sending HTML Email Using mail() .................................................................................................... 131
Section 25.3: Sending Email With An Attachment Using mail() ............................................................................ 132
Section 25.4: Sending Plain Text Email Using PHPMailer ...................................................................................... 133
Section 25.5: Sending HTML Email Using PHPMailer ............................................................................................ 134
Section 25.6: Sending Email With An Attachment Using PHPMailer .................................................................... 135
Section 25.7: Sending Plain Text Email Using Sendgrid ........................................................................................ 135
Section 25.8: Sending Email With An Attachment Using Sendgrid ...................................................................... 136
Chapter 26: Sessions ............................................................................................................................................... 137
Section 26.1: session_start() Options ...................................................................................................................... 137
Section 26.2: Session Locking .................................................................................................................................. 137
Section 26.3: Manipulating session data ................................................................................................................. 138
Section 26.4: Destroy an entire session .................................................................................................................. 138
Section 26.5: Safe Session Start With no Errors ..................................................................................................... 139
Section 26.6: Session name ...................................................................................................................................... 139
Chapter 27: Cookies ................................................................................................................................................ 141
Section 27.1: Modifying a Cookie ............................................................................................................................. 141
Section 27.2: Setting a Cookie ................................................................................................................................. 141
Section 27.3: Checking if a Cookie is Set ................................................................................................................. 142
Section 27.4: Removing a Cookie ............................................................................................................................ 142
Section 27.5: Retrieving a Cookie ............................................................................................................................ 142
Chapter 28: Classes and Objects ...................................................................................................................... 143
Section 28.1: Class Constants ................................................................................................................................... 143
Section 28.2: Abstract Classes ................................................................................................................................. 145
Section 28.3: Late static binding .............................................................................................................................. 147
Section 28.4: Namespacing and Autoloading ........................................................................................................ 148
Section 28.5: Method and Property Visibility .......................................................................................................... 150