About the Author |
|
vii | |
About the Technical Reviewer |
|
xix | |
Acknowledgments |
|
xxi | |
Introduction |
|
xxiii | |
Chapter 1 What Is PHP 8? |
|
1 | (8) |
|
|
2 | (1) |
|
How PHP Makes Pages Dynamic |
|
|
2 | (1) |
|
Creating Pages That Think for Themselves |
|
|
3 | (1) |
|
How Hard Is PHP to Use and Learn? |
|
|
3 | (2) |
|
Can I Just Copy and Paste the Code? |
|
|
4 | (1) |
|
|
5 | (1) |
|
|
5 | (1) |
|
What Software Do I Need to Write PHP? |
|
|
6 | (1) |
|
What to Look for When Choosing a PHP Editor |
|
|
6 | (1) |
|
|
7 | (2) |
Chapter 2 Getting Ready to Work with PHP |
|
9 | (12) |
|
Checking Whether Your Web Site Supports PHP |
|
|
9 | (1) |
|
Deciding Where to Test Your Pages |
|
|
10 | (1) |
|
What You Need for a Local Test Environment |
|
|
10 | (1) |
|
|
11 | (1) |
|
Getting Windows to Display Filename Extensions |
|
|
11 | (1) |
|
|
11 | (1) |
|
Installing an All-in-One Package on Windows |
|
|
11 | (1) |
|
|
11 | (3) |
|
|
12 | (1) |
|
Testing and Configuring MAMP |
|
|
12 | (2) |
|
Where to Locate Your PHP Files (Windows and Mac) |
|
|
14 | (1) |
|
Checking Your PHP Settings |
|
|
15 | (4) |
|
Displaying the Server Configuration with phpinfo() |
|
|
15 | (2) |
|
|
17 | (2) |
|
|
19 | (2) |
Chapter 3 How to Write PHP Scripts |
|
21 | (20) |
|
|
21 | (17) |
|
Telling the Server to Process PHP |
|
|
22 | (1) |
|
Embedding PHP in a Web Page |
|
|
22 | (1) |
|
Storing PHP in an External File |
|
|
23 | (1) |
|
Using Variables to Represent Changing Values |
|
|
23 | (3) |
|
Ending Commands with a Semicolon |
|
|
26 | (1) |
|
|
26 | (1) |
|
Using Arrays to Store Multiple Values |
|
|
27 | (2) |
|
PHP's Built-In Superglobal Arrays |
|
|
29 | (1) |
|
Understanding When to Use Quotes |
|
|
29 | (3) |
|
|
32 | (1) |
|
|
33 | (1) |
|
Using Indenting and Whitespace for Clarity |
|
|
34 | (1) |
|
Using Loops for Repetitive Tasks |
|
|
34 | (1) |
|
Using Functions for Preset Tasks |
|
|
34 | (1) |
|
|
35 | (2) |
|
Understanding PHP Error Messages |
|
|
37 | (1) |
|
|
38 | (3) |
Chapter 4 PHP: A Quick Reference |
|
41 | (40) |
|
Using PHP in an Existing Web Site |
|
|
41 | (1) |
|
|
41 | (3) |
|
Checking the Data Type of a Variable |
|
|
43 | (1) |
|
Explicitly Changing a Variable's Data Type |
|
|
43 | (1) |
|
Checking Whether a Variable Has Been Defined |
|
|
44 | (1) |
|
Doing Calculations with PHP |
|
|
44 | (2) |
|
|
44 | (1) |
|
Using the Increment and Decrement Operators |
|
|
45 | (1) |
|
Determining the Order of Calculations |
|
|
45 | (1) |
|
Combining Calculations and Assignment |
|
|
46 | (1) |
|
Adding to an Existing String |
|
|
46 | (1) |
|
All You Ever Wanted to Know About Quotes-and More |
|
|
47 | (4) |
|
How PHP Treats Variables Inside Strings |
|
|
47 | (1) |
|
Using Escape Sequences Inside Double Quotes |
|
|
47 | (1) |
|
Embedding Associative Array Elements in a String |
|
|
48 | (1) |
|
Avoiding the Need to Escape Quotes with Heredoc Syntax |
|
|
49 | (2) |
|
|
51 | (3) |
|
Building an Indexed Array |
|
|
51 | (1) |
|
Building an Associative Array |
|
|
52 | (1) |
|
|
52 | (1) |
|
|
53 | (1) |
|
Using print_r() to Inspect an Array |
|
|
53 | (1) |
|
The Truth According to PHP |
|
|
54 | (6) |
|
|
54 | (1) |
|
Implicit Boolean ("Truthy" and "Falsy") Values |
|
|
55 | (1) |
|
Making Decisions by Comparing Two Values |
|
|
55 | (1) |
|
Testing More Than One Condition |
|
|
56 | (1) |
|
Using the switch Statement for Decision Chains |
|
|
57 | (1) |
|
Using a match Expression for Decision Chains |
|
|
58 | (1) |
|
Using the Ternary Operator |
|
|
59 | (1) |
|
Setting a Default Value with the Null Coalescing Operator |
|
|
60 | (1) |
|
Executing Code Repeatedly with a Loop |
|
|
60 | (3) |
|
Loops Using while and do...while |
|
|
60 | (1) |
|
|
61 | (1) |
|
Looping Through Arrays and Objects with foreach |
|
|
62 | (1) |
|
|
63 | (1) |
|
Modularizing Code with Functions |
|
|
63 | (12) |
|
Passing Values to Functions |
|
|
64 | (1) |
|
Setting Default Values for Arguments |
|
|
65 | (1) |
|
Variable Scope: Functions as Black Boxes |
|
|
65 | (1) |
|
Returning Values from Functions |
|
|
66 | (1) |
|
Generators: A Special Type of Function That Keeps on Giving |
|
|
67 | (1) |
|
Passing by Reference: Changing the Value of an Argument |
|
|
68 | (1) |
|
Functions That Accept a Variable Number of Arguments |
|
|
68 | (1) |
|
Automatically Unpacking an Array Passed to a Function |
|
|
69 | (1) |
|
Optionally Specifying Data Types |
|
|
70 | (2) |
|
|
72 | (1) |
|
Where to Locate Custom-Built Functions |
|
|
73 | (1) |
|
Creating Anonymous Functions |
|
|
73 | (2) |
|
Understanding PHP Classes and Objects |
|
|
75 | (4) |
|
Using PHP Built-In Classes |
|
|
75 | (1) |
|
|
76 | (3) |
|
Handling Errors and Exceptions |
|
|
79 | (1) |
|
Creating New Variables Dynamically |
|
|
80 | (1) |
|
|
80 | (1) |
Chapter 5 Lightening Your Workload with Includes |
|
81 | (38) |
|
Including Code from External Files |
|
|
81 | (36) |
|
Introducing the PHP Include Commands |
|
|
82 | (1) |
|
Where PHP Looks for Include Files |
|
|
82 | (4) |
|
Choosing the Right Filename Extension for Includes |
|
|
86 | (12) |
|
Creating Pages with Changing Content |
|
|
98 | (9) |
|
Preventing Errors with Include Files |
|
|
107 | (1) |
|
Suppressing Error Messages on a Live Web Site |
|
|
108 | (5) |
|
Why Can't I Use Site Root-Relative Links with PHP Includes? |
|
|
113 | (1) |
|
Choosing Where to Locate Your Include Files |
|
|
114 | (1) |
|
Security Considerations with Includes |
|
|
114 | (1) |
|
Adjusting Your include_path |
|
|
115 | (2) |
|
|
117 | (2) |
Chapter 6 Bringing Forms to Life |
|
119 | (38) |
|
How PHP Gathers Information from a Form |
|
|
119 | (5) |
|
Understanding the Difference Between post and get |
|
|
121 | (3) |
|
Getting Form Data with PHP Superglobals |
|
|
124 | (1) |
|
Processing and Validating User Input |
|
|
124 | (13) |
|
Creating a Reusable Script |
|
|
125 | (7) |
|
Preserving User Input When a Form Is Incomplete |
|
|
132 | (3) |
|
Filtering Out Potential Attacks |
|
|
135 | (2) |
|
|
137 | (9) |
|
Using Additional Email Headers Safely |
|
|
137 | (9) |
|
Handling Multiple-Choice Form Elements |
|
|
146 | (9) |
|
PHP Solution 6-7: Handling Radio Button Groups |
|
|
148 | (7) |
|
|
155 | (2) |
Chapter 7 Using PHP to Manage Files |
|
157 | (34) |
|
Checking That PHP Can Open a File |
|
|
157 | (3) |
|
Creating a Folder Outside the Server Root for Local Testing on Windows |
|
|
158 | (1) |
|
Configuration Settings That Affect File Access |
|
|
159 | (1) |
|
Reading and Writing to Files |
|
|
160 | (13) |
|
Reading Files in a Single Operation |
|
|
160 | (4) |
|
Opening and Closing Files for Read/Write Operations |
|
|
164 | (9) |
|
Exploring the File System |
|
|
173 | (9) |
|
Inspecting a Folder with scandir0 |
|
|
174 | (1) |
|
Inspecting the Contents of a Folder with Filesystemiterator |
|
|
174 | (4) |
|
Restricting File Types with the RegexIterator |
|
|
178 | (4) |
|
|
182 | (6) |
|
Consuming News and Other RSS Feeds |
|
|
183 | (1) |
|
|
184 | (4) |
|
|
188 | (2) |
|
PHP Solution 7-6: Prompting a User to Download an Image |
|
|
188 | (2) |
|
|
190 | (1) |
Chapter 8 Working with Arrays |
|
191 | (44) |
|
|
191 | (8) |
|
PHP Solution 8-1: Modify Array Elements with a Loop |
|
|
192 | (2) |
|
PHP Solution 8-2: Modify Array Elements with array_walk() |
|
|
194 | (2) |
|
PHP Solution 8-3: Modify Array Elements with array_map() |
|
|
196 | (3) |
|
|
199 | (10) |
|
Using the Array Union Operator |
|
|
199 | (2) |
|
Using array_mergeO and array_merge_recursive0 |
|
|
201 | (2) |
|
Merging Two Indexed Arrays into an Associative Array |
|
|
203 | (1) |
|
|
204 | (1) |
|
Removing Duplicate Elements |
|
|
205 | (1) |
|
PHP Solution 8-4: Joining an Array with Commas |
|
|
206 | (3) |
|
|
209 | (10) |
|
PHP Solution 8-5: Custom Sorting with the Spaceship Operator |
|
|
211 | (2) |
|
Complex Sorting with array_multisort0 |
|
|
213 | (2) |
|
PHP Solution 8-6: Sorting a Multidimensional Array with array_multisorto |
|
|
215 | (2) |
|
PHP Solution 8-7: Finding All Permutations of an Array |
|
|
217 | (2) |
|
|
219 | (9) |
|
PHP Solution 8-8: Building Nested Lists Automatically |
|
|
219 | (4) |
|
PHP Solution 8-9: Extracting Data from JSON |
|
|
223 | (5) |
|
Automatically Assigning Array Elements to Variables |
|
|
228 | (4) |
|
Using the extract° Function |
|
|
228 | (1) |
|
|
229 | (1) |
|
Using Array Shorthand Syntax for listo |
|
|
229 | (1) |
|
PHP Solution 8-10: Using a Generator to Process a CSV File |
|
|
229 | (3) |
|
Unpacking Arguments from an Array with the Splat Operator |
|
|
232 | (2) |
|
PHP Solution 8-11: Processing a CSV File with the Splat Operator |
|
|
232 | (2) |
|
|
234 | (1) |
Chapter 9 Uploading Files |
|
235 | (34) |
|
How PHP Handles File Uploads |
|
|
235 | (5) |
|
Checking Whether Your Server Supports Uploads |
|
|
236 | (1) |
|
Adding a File Upload Field to a Form |
|
|
237 | (1) |
|
Understanding the $_FILES Array |
|
|
237 | (3) |
|
Establishing an Upload Directory |
|
|
240 | (1) |
|
|
240 | (4) |
|
Moving the Temporary File to the Upload Folder |
|
|
241 | (3) |
|
Creating a PHP File Upload Class |
|
|
244 | (16) |
|
PHP Solution 9-2: Creating the Basic File Upload Class |
|
|
244 | (6) |
|
|
250 | (3) |
|
Changing Protected Properties |
|
|
253 | (7) |
|
|
260 | (6) |
|
How the $_FILES Array Handles Multiple Files |
|
|
260 | (6) |
|
|
266 | (1) |
|
Points to Watch with File Uploads |
|
|
266 | (1) |
|
|
267 | (2) |
Chapter 10 Generating Thumbnail Images |
|
269 | (24) |
|
Checking Your Server's Capabilities |
|
|
269 | (1) |
|
Manipulating Images Dynamically |
|
|
270 | (16) |
|
Making a Smaller Copy of an Image |
|
|
270 | (16) |
|
Resizing an Image Automatically on Upload |
|
|
286 | (6) |
|
|
287 | (4) |
|
Using the ThumbnailUpload Class |
|
|
291 | (1) |
|
|
292 | (1) |
Chapter 11 Pages That Remember: Simple Login and Multipage Forms |
|
293 | (38) |
|
What Sessions Are and How They Work |
|
|
293 | (5) |
|
|
296 | (1) |
|
Creating and Destroying Session Variables |
|
|
296 | (1) |
|
|
297 | (1) |
|
Regenerating the Session ID |
|
|
297 | (1) |
|
The "Headers Already Sent" Error |
|
|
297 | (1) |
|
Using Sessions to Restrict Access |
|
|
298 | (23) |
|
PHP Solution 11-1: A Simple Session Example |
|
|
298 | (4) |
|
Using File-Based Authentication |
|
|
302 | (1) |
|
|
302 | (19) |
|
Setting a Time Limit on Sessions |
|
|
321 | (3) |
|
PHP Solution 11-8: Ending a Session After a Period of Inactivity |
|
|
321 | (3) |
|
Passing Information Through Multipage Forms |
|
|
324 | (5) |
|
PHP Solution 11-9: Using Sessions for a Multipage Form |
|
|
324 | (5) |
|
|
329 | (2) |
Chapter 12 Getting Started with a Database |
|
331 | (26) |
|
|
331 | (1) |
|
How a Database Stores Information |
|
|
332 | (3) |
|
|
333 | (1) |
|
Linking Tables with Primary and Foreign Keys |
|
|
334 | (1) |
|
Breaking Down Information into Small Chunks |
|
|
334 | (1) |
|
Checkpoints for Good Database Design |
|
|
335 | (1) |
|
Using a Graphical Interface |
|
|
335 | (2) |
|
|
336 | (1) |
|
Setting Up the phpsols Database |
|
|
337 | (17) |
|
|
337 | (1) |
|
Using phpMyAdmin to Create a New Database |
|
|
338 | (1) |
|
Creating Database-Specific User Accounts |
|
|
339 | (4) |
|
Creating a Database Table |
|
|
343 | (2) |
|
Inserting Records into a Table |
|
|
345 | (5) |
|
Creating an SQL File for Backup and Data Transfer |
|
|
350 | (4) |
|
Choosing the Right Data Type in MySQL |
|
|
354 | (2) |
|
|
354 | (1) |
|
|
355 | (1) |
|
|
355 | (1) |
|
|
356 | (1) |
|
|
356 | (1) |
|
|
356 | (1) |
Chapter 13 Connecting to a Database with PHP and SQL |
|
357 | (38) |
|
Checking Your Remote Server Setup |
|
|
357 | (1) |
|
How PHP Communicates with a Database |
|
|
358 | (12) |
|
Connecting with the MySQL Improved Extension |
|
|
359 | (1) |
|
|
359 | (1) |
|
PHP Solution 13-1: Making a Reusable Database Connector |
|
|
360 | (3) |
|
Sanitizing Text Results from a Database |
|
|
363 | (1) |
|
Querying the Database and Displaying the Results |
|
|
363 | (7) |
|
Using SQL to Interact with a Database |
|
|
370 | (6) |
|
|
370 | (2) |
|
Refining the Data Retrieved by a SELECT Query |
|
|
372 | (4) |
|
Understanding the Danger of SQL Injection |
|
|
376 | (5) |
|
PHP Solution 13-6: Inserting an Integer from User Input into a Query |
|
|
376 | (5) |
|
Using Prepared Statements for User Input |
|
|
381 | (12) |
|
Embedding Variables in MySQLi Prepared Statements |
|
|
381 | (5) |
|
Embedding Variables in PDO Prepared Statements |
|
|
386 | (7) |
|
|
393 | (2) |
Chapter 14 Creating a Dynamic Photo Gallery |
|
395 | (20) |
|
Why Not Store Images in a Database? |
|
|
396 | (1) |
|
|
396 | (2) |
|
Converting the Gallery Elements to PHP |
|
|
398 | (3) |
|
PHP Solution 14-1: Displaying the First Image |
|
|
398 | (3) |
|
Building the Dynamic Elements |
|
|
401 | (13) |
|
Passing Information Through a Query String |
|
|
401 | (4) |
|
Creating a Multicolumn Table |
|
|
405 | (2) |
|
Paging Through a Long Set of Records |
|
|
407 | (7) |
|
|
414 | (1) |
Chapter 15 Managing Content |
|
415 | (28) |
|
Setting Up a Content Management System |
|
|
415 | (22) |
|
Creating the Blog Database Table |
|
|
416 | (2) |
|
Creating the Basic Insert and Update Forms |
|
|
418 | (1) |
|
|
419 | (4) |
|
Linking to the Update and Delete Pages |
|
|
423 | (4) |
|
|
427 | (9) |
|
|
436 | (1) |
|
Reviewing the Four Essential SQL Commands |
|
|
437 | (4) |
|
|
438 | (2) |
|
|
440 | (1) |
|
|
440 | (1) |
|
|
441 | (1) |
|
Security and Error Messages |
|
|
441 | (1) |
|
|
441 | (2) |
Chapter 16 Formatting Text and Dates |
|
443 | (36) |
|
Displaying a Text Extract |
|
|
443 | (8) |
|
Extracting a Fixed Number of Characters |
|
|
443 | (2) |
|
Ending an Extract on a Complete Word |
|
|
445 | (1) |
|
Extracting the First Paragraph |
|
|
446 | (1) |
|
|
446 | (2) |
|
Extracting Complete Sentences |
|
|
448 | (3) |
|
|
451 | (26) |
|
|
452 | (4) |
|
Inserting Dates into MySQL |
|
|
456 | (5) |
|
Working with Dates in PHP |
|
|
461 | (16) |
|
|
477 | (2) |
Chapter 17 Pulling Data from Multiple Tables |
|
479 | (20) |
|
Understanding Table Relationships |
|
|
479 | (2) |
|
Linking an Image to an Article |
|
|
481 | (17) |
|
Altering the Structure of an Existing Table |
|
|
481 | (2) |
|
Inserting a Foreign Key in a Table |
|
|
483 | (6) |
|
Selecting Records from Multiple Tables |
|
|
489 | (6) |
|
Finding Records That Don't Have a Matching Foreign Key |
|
|
495 | (2) |
|
Creating an Intelligent Link |
|
|
497 | (1) |
|
|
498 | (1) |
Chapter 18 Managing Multiple Database Tables |
|
499 | (38) |
|
Maintaining Referential Integrity |
|
|
499 | (19) |
|
Support for Transactions and Foreign-key Constraints |
|
|
500 | (3) |
|
Inserting Records into Multiple Tables |
|
|
503 | (2) |
|
Creating a Cross-reference Table |
|
|
505 | (1) |
|
Getting the Filename of an Uploaded Image |
|
|
506 | (1) |
|
Adapting the Insert Form to Deal with Multiple Tables |
|
|
507 | (11) |
|
Updating and Deleting Records in Multiple Tables |
|
|
518 | (17) |
|
Updating Records in a Cross-Reference Table |
|
|
518 | (3) |
|
Treating Multiple Queries as a Block in a Transaction |
|
|
521 | (8) |
|
Preserving Referential Integrity on Deletion |
|
|
529 | (4) |
|
Creating Delete Scripts with Foreign-Key Constraints |
|
|
533 | (1) |
|
Creating Delete Scripts Without Foreign-Key Constraints |
|
|
534 | (1) |
|
|
535 | (2) |
Chapter 19 Authenticating Users with a Database |
|
537 | (14) |
|
Choosing a Password Storage Method |
|
|
537 | (1) |
|
|
538 | (8) |
|
Creating a Table to Store Users' Details |
|
|
538 | (1) |
|
Registering New Users in the Database |
|
|
538 | (8) |
|
Using Secret-Key Encryption |
|
|
546 | (4) |
|
Creating the Table to Store Users' Details |
|
|
546 | (1) |
|
|
546 | (2) |
|
User Authentication with Two-Way Encryption |
|
|
548 | (1) |
|
|
549 | (1) |
|
|
550 | (1) |
|
|
550 | (1) |
Index |
|
551 | |