1.Introduction to PHP 1
What Does PHP Do? 1
A Brief History of PHP 2
The Evolution of PHP 2
The Widespread Use of PHP 6
Installing PHP 7
A Walk Through PHP 7
Configuration Page 8
Forms 9
Databases 10
Graphics 13
2.Language Basics 15
Lexical Structure 15
Case Sensitivity 15
Statements and Semicolons 15
Whitespace and Line Breaks 16
Comments 17
Literals 20
Identifiers 20
Keywords 21
Data Types 22
Integers 22
Floating-Point Numbers 23
Strings 24
Booleans 25
Arrays 26
Objects 27
Resources 28
Callbacks 29
NULL 29
Variables 29
Variable Variables 30
Variable References 30
Variable Scope 31
Garbage Collection 33
Expressions and Operators 34
Number of Operands 36
Operator Precedence 36
Operator Associativity 37
Implicit Casting 37
Arithmetic Operators 38
String Concatenation Operator 38
Auto-increment and Auto-decrement Operators 39
Comparison Operators 40
Bitwise Operators 41
Logical Operators 43
Casting Operators 43
Assignment Operators 45
Miscellaneous Operators 46
Flow-Control Statements 47
if 47
switch 49
while 51
for 53
foreach 54
try...catch 55
declare 55
exit and return 56
goto 56
Including Code 57
Embedding PHP in Web Pages 58
Standard(XML)Style 59
SGML Style 60
ASP Style 61
Script Style 61
Echoing Content Directly 61
3.Functions 63
Calling a Function 63
Defining a Function 64
Variable Scope 66
Global Variables 67
Static Variables 68
Function Parameters 68
Passing Parameters by Value 69
Passing Parameters by Reference 69
Default Parameters 70
Variable Parameters 70
Missing Parameters 71
Type Hinting 72
Return Values 72
Variable Functions 73
Anonymous Functions 74
4.Strings 77
Quoting String Constants 77
Variable Interpolation 77
Single-Quoted Strings 78
Double-Quoted Strings 78
Here Documents 79
Printing Strings 80
echo 81
print() 81
printf() 81
print_r() and var_dump() 83
Accessing Individual Characters 85
Cleaning Strings 85
Removing Whitespace 85
Changing Case 86
Encoding and Escaping 86
HTML 87
URLs 89
SQL 90
C-String Encoding 91
Comparing Strings 92
Exact Comparisons 92
Approximate Equality 93
Manipulating and Searching Strings 94
Substrings 95
Miscellaneous String Functions 96
Decomposing a String 97
String-Searching Functions 98
Regular Expressions 100
The Basics 101
Character Classes 102
Alternatives 103
Repeating Sequences 103
Subpatterns 104
Delimiters 104
Match Behavior 105
Character Classes 105
Anchors 106
Quantifiers and Greed 107
Noncapturing Groups 108
Backreferences 108
Trailing Options 108
Inline Options 109
Lookahead and Lookbehind 110
Cut 111
Conditional Expressions 112
Functions 112
Differences from Perl Regular Expressions 117
5.Arrays 119
Indexed Versus Associative Arrays 119
Identifying Elements of an Array 120
Storing Data in Arrays 120
Adding Values to the End of an Array 122
Assigning a Range of Values 122
Getting the Size of an Array 122
Padding an Array 122
Multidimensional Arrays 123
Extracting Multiple Values 123
Slicing an Array 124
Splitting an Array into Chunks 125
Keys and Values 125
Checking Whether an Element Exists 126
Removing and Inserting Elements in an Array 126
Converting Between Arrays and Variables 128
Creating Variables from an Array 128
Creating an Array from Variables 128
Traversing Arrays 129
The foreach Construct 129
The Iterator Functions 130
Using a for Loop 131
Calling a Function for Each Array Element 131
Reducing an Array 132
Searching for Values 133
Sorting 134
Sorting One Array at a Time 135
Natural-Order Sorting 137
Sorting Multiple Arrays at Once 137
Reversing Arrays 138
Randomizing Order 139
Acting on Entire Arrays 139
Calculating the Sum of an Array 139
Merging Two Arrays 140
Calculating the Difference Between Two Arrays 140
Filtering Elements from an Array 141
Using Arrays 141
Sets 141
Stacks 142
Iterator Interface 143
6.Objects 147
Terminology 148
Creating an Object 148
Accessing Properties and Methods 149
Declaring a Class 150
Declaring Methods 151
Declaring Properties 153
Declaring Constants 155
Inheritance 155
Interfaces 156
Traits 157
Abstract Methods 160
Constructors 161
Destructors 162
Introspection 163
Examining Classes 163
Examining an Object 164
Sample Introspection Program 165
Serialization 169
7.Web Techniques 173
HTTP Basics 173
Variables 174
Server Information 175
Processing Forms 177
Methods 177
Parameters 178
Self-Processing Pages 180
Sticky Forms 182
Multivalued Parameters 182
Sticky Multivalued Parameters 185
File Uploads 186
Form Validation 187
Setting Response Headers 189
Different Content Types 190
Redirections 190
Expiration 191
Authentication 191
Maintaining State 192
Cookies 193
Sessions 197
Combining Cookies and Sessions 199
SSL 200
8.Databases 203
Using PHP to Access a Database 203
Relational Databases and SQL 204
PHP Data Objects 205
MySQLi Object Interface 208
Retrieving Data for Display 209
SQLite 211
Direct File-Level Manipulation 214
MongoDB 222
Retrieving Data 224
Inserting More Complex Data 226
9.Graphics 229
Embedding an Image in a Page 229
Basic Graphics Concepts 230
Creating and Drawing Images 231
The Structure of a Graphics Program 232
Changing the Output Format 233
Testing for Supported Image Formats 233
Reading an Existing File 234
Basic Drawing Functions 234
Images with Text 236
Fonts 236
TrueType Fonts 237
Dynamically Generated Buttons 239
Caching the Dynamically Generated Buttons 240
A Faster Cache 241
Scaling Images 243
Color Handling 244
Using the Alpha Channel 245
Identifying Colors 246
True Color Indexes 247
Text Representation of an Image 248
10.PDF 251
PDF Extensions 251
Documents and Pages 251
A Simple Example 252
Initializing the Document 252
Outputting Basic Text Cells 253
Text 253
Coordinates 253
Text Attributes 255
Page Headers,Footers,and Class Extension 258
Images and Links 260
Tables and Data 263
11.XML 267
Lightning Guide to XML 267
Generating XML 269
Parsing XML 270
Element Handlers 271
Character Data Handler 272
Processing Instructions 272
Entity Handlers 273
Default Handler 275
Options 275
Using the Parser 276
Errors 278
Methods as Handlers 278
Sample Parsing Application 279
Parsing XML with DOM 283
Parsing XML with SimpleXML 284
Transforming XML with XSLT 285
12.Security 289
Filter Input 289
Cross-Site Scripting 292
SQL Injection 292
Escape Output 294
Filenames 298
Session Fixation 299
File Uploads 300
Distrust Browser-Supplied Filenames 300
Beware of Filling Your Filesystem 301
Surviving register_globals 301
File Access 301
Restrict Filesystem Access to a Specific Directory 302
Get It Right the First Time 302
Don't Use Files 303
Session Files 303
Concealing PHP Libraries 304
PHP Code 304
Shell Commands 305
More Information 306
Security Recap 306
13.Application Techniques 309
Code Libraries 309
Templating Systems 310
Handling Output 313
Output Buffering 313
Compressing Output 315
Error Handling 315
Error Reporting 316
Error Suppression 317
Triggering Errors 317
Defining Error Handlers 318
Performance Tuning 321
Benchmarking 322
Profiling 324
Optimizing Execution Time 325
Optimizing Memory Requirements 325
Reverse Proxies and Replication 326
14.PHP on Disparate Platforms 329
Writing Portable Code for Windows and Unix 329
Determining the Platform 330
Handling Paths Across Platforms 330
The Server Environment 330
Sending Mail 331
End-of-Line Handling 331
End-of-File Handling 332
External Commands 332
Common Platform-Specific Extensions 332
Interfacing with COM 333
Background 333
PHP Functions 335
Determining the API 335
15.Web Services 337
REST Clients 337
Responses 339
Retrieving Resources 341
Updating Resources 342
Creating Resources 343
Deleting Resources 344
XML-RPC 344
Servers 344
Clients 346
16.Debugging PHP 349
The Development Environment 349
The Staging Environment 350
The Production Environment 351
php.ini Settings 351
Manual Debugging 353
Error Log 355
IDE Debugging 355
Additional Debugging Techniques 357
17.Dates and Times 359
Appendix:Function Reference 363
Index 491