PHP Notes for Professionals

Programming, PHP · notes

Browse all développement web documents

PHP

Notes for ProfessionalsPHP

Notes for Professionals

400+ pages

of professional hints and tricks

GoalKicker.com

Free Programming Books

Disclaimer

This is an unocial free book created for educational purposes and is

not aliated with ocial PHP group(s) or company(s).

All trademarks and registered trademarks are

the property of their respective owners

Contents

About

...................................................................................................................................................................................

1

...................................................................................................................

..................................................................................................................

Chapter 1: Getting started with PHP

Section 1.1: HTML output from web server

Section 1.2: Hello, World!

Section 1.3: Non-HTML output from web server

Section 1.4: PHP built-in server

Section 1.5: PHP CLI

Section 1.6: Instruction Separation

Section 1.7: PHP Tags

Chapter 2: Variables

...............................................................................................................................................

........................................................................................................

3

.....................................................................................................................................

.......................................................................................................................................................

...............................................................................................................................

....................................................................................................................................................

....................................................................................................................................................

Section 2.1: Accessing A Variable Dynamically By Name (Variable variables)

Section 2.2: Data Types

Section 2.3: Global variable best practices

Section 2.4: Default values of uninitialized variables

Section 2.5: Variable Value Truthiness and Identical Operator

...............................................................................................................

..............................................................................................

.............................................................................

..............................................................................................................................................

......................................................

9

Chapter 3: Variable Scope

.....................................................................................................................................

18

Section 3.1: Superglobal variables

Section 3.2: Static properties and variables

Section 3.3: User-defined global variables

Chapter 4: Superglobal Variables PHP

.............................................................................................................................

.............................................................................................................

...............................................................................................................

...........................................................................................................

21

Section 4.1: Suberglobals explained

Section 4.2: PHP5 SuperGlobals

..........................................................................................................................

21

.................................................................................................................................

28

Chapter 5: Outputting the Value of a Variable

..........................................................................................

32

..........................................................................................................................................

Section 5.1: echo and print

Section 5.2: Outputting a structured view of arrays and objects

Section 5.3: String concatenation with echo

Section 5.4: printf vs sprintf

Section 5.5: Outputting large integers

Section 5.6: Output a Multidimensional Array with index and value and print into the table

........................................................................................................................................

......................................................................................................................

.............................................................................................................

..........................................................................

.............................

37

Chapter 6: Constants

................................................................................................................................................

39

Section 6.1: Defining constants

Section 6.2: Class Constants

Section 6.3: Checking if constant is defined

Section 6.4: Using constants

Section 6.5: Constant arrays

Chapter 7: Magic Constants

...................................................................................................................................

39

......................................................................................................................................

40

.............................................................................................................

40

......................................................................................................................................

......................................................................................................................................

..................................................................................................................................

Section 7.1: Dierence between __FUNCTION__ and __METHOD__

Section 7.2: Dierence between __CLASS__, get_class() and get_called_class()

Section 7.3: File & Directory Constants

.................................................................

...........................................

43

.....................................................................................................................

44

Chapter 8: Comments

..............................................................................................................................................

45

Section 8.1: Single Line Comments

Section 8.2: Multi Line Comments

............................................................................................................................

45

..............................................................................................................................

45

Chapter 9: Types

.........................................................................................................................................................

46

Section 9.1: Type Comparison

Section 9.2: Boolean

Section 9.3: Float

....................................................................................................................................

46

....................................................................................................................................................

.........................................................................................................................................................

2

2

3

5

5

6

Advertisement

7

9

10

13

14

15

18

18

19

32

33

35

36

36

42

42

43

43

46

47

Section 9.4: Strings

Section 9.5: Callable

Section 9.6: Resources

Section 9.7: Type Casting

Section 9.8: Type Juggling

Section 9.9: Null

Section 9.10: Integers

Chapter 10: Operators

......................................................................................................................................................

....................................................................................................................................................

................................................................................................................................................

...........................................................................................................................................

.........................................................................................................................................

51

...........................................................................................................................................................

..................................................................................................................................................

..............................................................................................................................................

..............................................................................................................

....................................................................................................................

..........................................................................................................................

.......................................................................

55

...........................................................................................................................

.............................................................................................

....................................................................................................................

.................................................................................................................

...........................................................................................................

...........................................................................

Section 10.1: Null Coalescing Operator (??)

Section 10.2: Spaceship Operator (<=>)

Section 10.3: Execution Operator (``)

Section 10.4: Incrementing (++) and Decrementing Operators (--)

Section 10.5: Ternary Operator (?:)

Section 10.6: Logical Operators (&&/AND and ||/OR)

Section 10.7: String Operators (. and .=)

Section 10.8: Object and Class Operators

Section 10.9: Combined Assignment (+= etc)

Section 10.10: Altering operator precedence (with parentheses)

Section 10.11: Basic Assignment (=)

Section 10.12: Association

Section 10.13: Comparison Operators

Section 10.14: Bitwise Operators

Section 10.15: instanceof (type operator)

........................................................................................................................

.................................................................................................................................

.................................................................................................................

............................................................................................................................

............................................................................................................................................

60

Chapter 11: References

............................................................................................................................................

67

Section 11.1: Assign by Reference

Section 11.2: Return by Reference

Section 11.3: Pass by Reference

...............................................................................................................................

67

..............................................................................................................................

67

.................................................................................................................................

68

Chapter 12: Arrays

......................................................................................................................................................

71

Section 12.1: Initializing an Array

Section 12.2: Check if key exists

Section 12.3: Validating the array type

Section 12.4: Creating an array of variables

Section 12.5: Checking if a value exists in array

Section 12.6: ArrayAccess and Iterator Interfaces

................................................................................................................................

.................................................................................................................................

.....................................................................................................................

............................................................................................................

.......................................................................................................

...................................................................................................

75

Chapter 13: Array iteration

....................................................................................................................................

79

Section 13.1: Iterating multiple arrays together

Section 13.2: Using an incremental index

Section 13.3: Using internal array pointers

Section 13.4: Using foreach

Section 13.5: Using ArrayObject Iterator

Chapter 14: Executing Upon an Array

........................................................................................................

79

..................................................................................................................

...............................................................................................................

.........................................................................................................................................

...................................................................................................................

..............................................................................................................

..............................................................................

84

..........................................................................................................................

.............................................................................................................

.......................................................................................................

Section 14.1: Applying a function to each element of an array

Section 14.2: Split array into chunks

Section 14.3: Imploding an array into string

Section 14.4: "Destructuring" arrays using list()

Section 14.5: array_reduce

Section 14.6: Push a Value on an Array

Chapter 15: Manipulating an Array

.........................................................................................................................................

.....................................................................................................................

....................................................................................................................

Section 15.1: Filtering an array

Section 15.2: Removing elements from an array

....................................................................................................................................

Advertisement

.....................................................................................................

90

48

50

50

51

52

52

54

54

55

55

56

57

57

57

59

59

60

60

62

64

71

73

74

74

74

80

80

81

83

84

85

86

86

86

87

89

89

....................................................................................................................................

Section 15.3: Sorting an Array

Section 15.4: Whitelist only some array keys

Section 15.5: Adding element to start of array

Section 15.6: Exchange values with keys

Section 15.7: Merge two arrays into one array

...........................................................................................................

.........................................................................................................

...................................................................................................................

........................................................................................................

97

Chapter 16: Processing Multiple Arrays Together

.....................................................................................

99

Section 16.1: Array intersection

Section 16.2: Merge or concatenate arrays

Section 16.3: Changing a multidimensional array to associative array

Section 16.4: Combining two arrays (keys from one, values from another)

...................................................................................................................................

..............................................................................................................

..............................................................

100

......................................................

100

Chapter 17: Datetime Class

.................................................................................................................................

102

Section 17.1: Create Immutable version of DateTime from Mutable prior PHP 5.6

Section 17.2: Add or Subtract Date Intervals

Section 17.3: getTimestamp

Section 17.4: setDate

Section 17.5: Create DateTime from custom format

Section 17.6: Printing DateTimes

.....................................................................................................................................

.................................................................................................................................................

..............................................................................................................................

..........................................................................................................

.............................................................................................

............................................

102

Chapter 18: Working with Dates and Time

..................................................................................................

105

Section 18.1: Getting the dierence between two dates / times

Section 18.2: Convert a date into another format

Section 18.3: Parse English date descriptions into a Date format

Section 18.4: Using Predefined Constants for Date Format

..........................................................................

.................................................................................................

........................................................................

107

.................................................................................

107

Chapter 19: Control Structures

..........................................................................................................................

109

..........................................................................................

109

.....................................................................................................................................................

109

Section 19.1: if else

Section 19.2: Alternative syntax for control structures

Section 19.3: while

Section 19.4: do-while

Section 19.5: goto

Section 19.6: declare

Section 19.7: include & require

Section 19.8: return

Section 19.9: for

Section 19.10: foreach

Section 19.11: if elseif else

Section 19.12: if

Section 19.13: switch

Chapter 20: Loops

......................................................................................................................................................

................................................................................................................................................

......................................................................................................................................................

..................................................................................................................................................

.................................................................................................................................

....................................................................................................................................................

.........................................................................................................................................................

................................................................................................................................................

..........................................................................................................................................

...........................................................................................................................................................

...................................................................................................................................................

....................................................................................................................................................

...............................................................................................................................................

...................................................................................................................................................

................................................................................................................................................

..............................................................................................................................................

........................................................................................................................................................

.....................................................................................................................................................

Advertisement

Section 20.1: continue

Section 20.2: break

Section 20.3: foreach

Section 20.4: do...while

Section 20.5: for

Section 20.6: while

Chapter 21: Functions

.............................................................................................................................................

121

Section 21.1: Variable-length argument lists

Section 21.2: Optional Parameters

Section 21.3: Passing Arguments by Reference

Section 21.4: Basic Function Usage

Section 21.5: Function Scope

...........................................................................................................

..........................................................................................................................

.....................................................................................................

123

.........................................................................................................................

....................................................................................................................................

91

96

96

97

99

99

102

102

103

103

103

105

105

109

110

110

110

111

112

112

113

113

114

114

116

116

117

118

118

119

120

121

122

124

124

Chapter 22: Functional Programming

............................................................................................................

125

................................................................................................................................................

....................................................................................................................

.........................................................................................................................

.....................................................................................................................

..........................................................................................................................

.....................................................................................................................................

Section 22.1: Closures

Section 22.2: Assignment to variables

Section 22.3: Objects as a function

Section 22.4: Using outside variables

Section 22.5: Anonymous function

Section 22.6: Pure functions

Section 22.7: Common functional methods in PHP

Section 22.8: Using built-in functions as callbacks

Section 22.9: Scope

Section 22.10: Passing a callback function as a parameter

Chapter 23: Alternative Syntax for Control Structures

...............................................................................................

................................................................................................

...................................................................................................................................................

.................................................................................

........................................................................

131

Section 23.1: Alternative if/else statement

Section 23.2: Alternative for statement

Section 23.3: Alternative while statement

Section 23.4: Alternative foreach statement

Section 23.5: Alternative switch statement

.............................................................................................................

..................................................................................................................

...............................................................................................................

..........................................................................................................

131

.............................................................................................................

132

Chapter 24: String formatting

..........................................................................................................................

133

Section 24.1: String interpolation

Section 24.2: Extracting/replacing substrings

.............................................................................................................................

133

.......................................................................................................

134

Chapter 25: String Parsing

...................................................................................................................................

136

Section 25.1: Splitting a string by separators

Section 25.2: Substring

Section 25.3: Searching a substring with strpos

Section 25.4: Parsing string using regular expressions

.............................................................................................................................................

.........................................................................................................

....................................................................................................

.........................................................................................

139

Chapter 26: Classes and Objects

......................................................................................................................

140

...................................................................................................................................

.................................................................................................................................

..............................................................................................................................

........................................................................................................

..........................................................................................................

............................................................................................................................................

.....................................................................................................................................

.........................................................................................................................................

Section 26.1: Class Constants

Section 26.2: Abstract Classes

Section 26.3: Late static binding

Section 26.4: Namespacing and Autoloading

Section 26.5: Method and Property Visibility

Section 26.6: Interfaces

Advertisement

Section 26.7: Final Keyword

Section 26.8: Autoloading

Section 26.9: Calling a parent constructor when instantiating a child

Section 26.10: Dynamic Binding

Section 26.11: $this, self and static plus the singleton

Section 26.12: Defining a Basic Class

Section 26.13: Anonymous Classes

...............................................................................................................................

............................................................................................

......................................................................................................................

..........................................................................................................................

................................................................

154

Chapter 27: Namespaces

.....................................................................................................................................

162

Section 27.1: Declaring namespaces

Section 27.2: Referencing a class or function in a namespace

Section 27.3: Declaring sub-namespaces

Section 27.4: What are Namespaces?

.......................................................................................................................

162

...........................................................................

162

...............................................................................................................

....................................................................................................................

Chapter 28: Sessions

...............................................................................................................................................

165

Section 28.1: session_start() Options

Section 28.2: Session Locking

Section 28.3: Manipulating session data

Section 28.4: Destroy an entire session

......................................................................................................................

..................................................................................................................................

.................................................................................................................

166

..................................................................................................................

166

125

126

126

127

127

128

128

129

129

129

131

131

131

136

136

138

140

142

144

145

147

149

152

153

155

156

159

160

163

164

165

165

Section 28.5: Safe Session Start With no Errors

Section 28.6: Session name

.....................................................................................................

167

......................................................................................................................................

167

Chapter 29: Cookies

.................................................................................................................................................

169

Section 29.1: Modifying a Cookie

Section 29.2: Setting a Cookie

Section 29.3: Checking if a Cookie is Set

Section 29.4: Removing a Cookie

Section 29.5: Retrieving a Cookie

Chapter 30: Output Buering

.............................................................................................................................

.................................................................................................................................

................................................................................................................

170

............................................................................................................................

............................................................................................................................

............................................................................................................................

................................................................................................

........................................................................................................................

Section 30.1: Basic usage getting content between buers and clearing

Section 30.2: Processing the buer via a callback

Section 30.3: Nested output buers

Section 30.4: Running output buer before any content

Section 30.5: Stream output to client

Section 30.6: Using Output buer to store contents in a file, useful for reports, invoices etc

Section 30.7: Typical usage and reasons for using ob_start

Section 30.8: Capturing the output buer to re-use later

...............................................................................

....................................................................................

......................................................................................................................

.....................................................................................

..........................................................

..........................

174

Chapter 31: JSON

......................................................................................................................................................

177

Section 31.1: Decoding a JSON string

Section 31.2: Encoding a JSON string

Section 31.3: Debugging JSON errors

Section 31.4: Using JsonSerializable in an Object

Section 31.5: Header json and the returned response

......................................................................................................................

......................................................................................................................

.....................................................................................................................

..................................................................................................

...........................................................................................

185

Chapter 32: SOAP Client

Section 32.1: WSDL Mode

Section 32.2: Non-WSDL Mode

Section 32.3: Classmaps

Section 32.4: Tracing SOAP request and response

........................................................................................................................................

................................................................................................................................