Cover....1
Title Page....4
Copyright....5
Dedication....6
About the Author....8
About the Technical Reviewers....8
Brief Contents....10
Contents in Detail....12
Acknowledgments....24
Introduction....26
Who This Book Is For....27
Why PHP?....27
What You Will Learn....27
Online Resources....30
Part I: Language Fundamentals....32
1. PHP Program Basics....34
Two Methods to Run PHP....34
The Replit Online Coding Environment....35
A Local PHP Installation....39
Template Text vs. PHP Code....44
Comments....46
Variables....47
Creating Variables....48
Using Variables....49
Naming Variables....49
Constants....51
Operators and Operands....52
Arithmetic Operators....52
Combined Arithmetic Assignment Operators....53
Increment and Decrement Operators....54
Summary....55
Exercises....55
2. Data Types....58
PHP Data Types....59
Scalar Data Types....59
The Special NULL Type....61
Functions to Test for a Data Type....62
Type Juggling....63
Numeric Contexts....64
String Contexts....66
Comparative Contexts....66
Logical and Other Contexts....70
Type Casting....70
Summary....71
Exercises....71
3. Strings and String Functions....72
Whitespace....73
Single-Quoted Strings....74
Joining Strings: Concatenation....75
Double-Quoted Strings....77
Handling the Character After a Variable Name....78
Incorporating Unicode Characters....79
Heredocs....80
Escape Sequences....81
Indention....81
Nowdocs....83
Built-in String Functions....84
Converting to Upper- and Lowercase....84
Searching and Counting....85
Extracting and Replacing Substrings....87
Trimming Whitespace....90
Removing All Unnecessary Whitespace....91
Repeating and Padding....92
Summary....93
Exercises....93
4. Conditionals....96
Conditions Are True or False....97
if Statements....97
if...else Statements....98
Nested if...else Statements....99
if...elseif...else Statements....99
Alternative Syntax....100
Logical Operators....101
NOT....101
AND....102
OR....103
XOR....104
switch Statements....104
match Statements....106
The Ternary Operator....107
The Null-Coalescing Operator....108
Summary....109
Exercises....110
5. Custom Functions....112
Separating Code into Multiple Files....113
Reading in and Executing Another Script....113
Creating Absolute Filepaths....114
Declaring and Calling a Function....115
Parameters vs. Arguments....117
Errors from Incorrect Function Calls....118
Type Juggling....120
Functions Without Explicit Return Values....121
Returning NULL....122
Exiting a Function Early....123
Calling Functions from Within Functions....124
Functions with Multiple Return and Parameter Types....126
Nullable Types....126
Union Types....129
Optional Parameters....131
Positional vs. Named Arguments....133
Skipped Parameters....135
Pass-by-Value vs. Pass-by-Reference....136
Summary....138
Exercises....139
Part II: Working with Data....140
6. Loops....142
while Loops....143
do... while Loops....144
Boolean Flags....145
break Statements....147
for Loops....148
Using the Counter in the Loop....149
Skipping Loop Statements....150
Handling the Last Iteration Differently....151
Alternative Loop Syntax....153
Avoiding Infinite Loops....154
Summary....155
Exercises....155
7. Simple Arrays....156
Creating an Array and Accessing Its Values....157
Updating an Array....158
Appending an Element....158
Adding an Element with a Specific Key....159
Appending Multiple Elements....160
Removing the Last Element....161
Retrieving Information About an Array....162
Looping Through an Array....164
Using a foreach Loop....165
Accessing Keys and Values....165
Imploding an Array....166
Functions with a Variable Number of Arguments....167
Array Copies vs. Array References....168
Treating Strings as Arrays of Characters....170
Other Array Functions....172
Summary....172
Exercises....172
8. Sophisticated Arrays....174
Declaring Array Keys Explicitly....175
Arrays with Strings as Keys....177
Multidimensional Arrays....178
More Array Operations....180
Removing Any Element from an Array....180
Combining and Comparing Arrays....181
Destructuring an Array into Multiple Variables....183
Callback Functions and Arrays....184
Summary....186
Exercises....186
9. Files and Directories....188
Reading a File into a String....189
Confirming That a File Exists....190
Touching a File....192
Ensuring That a Directory Exists....192
Writing a String to a Text File....194
Managing Files and Directories....195
Reading a File into an Array....197
Using Lower-Level File Functions....197
Processing Multiple Files....199
JSON and Other File Types....202
Summary....204
Exercises....204
Part III: Programming Web Applications....206
10. ClientServer Communication and Web Development Basics....208
The HTTP Request-Response Cycle....209
Response Status Codes....210
An Example GET Request....211
How Servers Operate....213
Simple Web Servers for File Retrieval....213
Dynamic Web Servers for Processing Data....214
The Routing Process....215
Templating....216
PHP Tags....218
Short Echo Tags....219
The Model-View-Controller Architecture....219
Structuring a PHP Web Development Project....221
Summary....224
Exercises....224
11. Creating and Processing Web Forms....226
Basic ClientServer Communication for Web Forms....227
GET vs. POST Requests....228
A Simple Example....230
The filter_input() Function....235
Other Ways to Send Data....237
Sending Noneditable Data Along with Form Variables....237
Processing Mixed Query-String and POST Variables....238
Offering Multiple Submit Buttons....239
Encoding Data in Hyperlinks....242
Other Form Input Types....245
Radio Buttons....246
Checkboxes....247
Single-Selection Lists....251
Multiple-Selection Lists....252
Summary....254
Exercises....254
12. Validating Form Data....256
Writing Custom Validation Logic....257
Managing Multiple Validation Errors....258
Testing for a Valid Zero Value....259
Displaying and Validating Forms in a Single Postback Script....261
Simple Validation Logic....262
Array-Based Validation Logic....266
Summary....269
Exercises....269
13. Organizing a Web Application....270
Front Controllers and the MVC Architecture....271
Separating Display and Logic Files....273
Creating the Front Controller....274
Writing the Display Scripts....274
Moving Website Logic into Functions....276
Designing a Secure Folder Structure....276
Simplifying the Front-Controller Script....277
Writing the Functions....277
Generalizing the Front-Controller Structure....279
Distinguishing Between Requested Pages....280
Building a Multipage Application....280
Summary....288
Exercises....288
Part IV: Storing User Data with Browser Sessions....290
14. Working with Sessions....292
A Web Browser Session....293
The session_start() and session_id() Functions....295
The _SESSION Superglobal Array....296
Updating a Stored Value....297
Unsetting a Value....300
Destroying the Session and Emptying the Session Array....301
Summary....304
Exercises....304
15. Implementing a Shopping Cart....306
The Shopping Cart File Structure....307
Defining the Product List....308
Creating the Products Array....309
Adding CSS....310
Displaying the Star Ratings....311
Creating the Template Script....312
Updating the Index Script....313
Designing the Shopping Cart....314
Creating the Front Controller....315
Managing the Product and Cart Arrays....315
Streamlining the Index Script....317
Creating a Header Template....318
Creating the Cart Display Template....319
Interacting with the Session....323
Updating the Cart-Retrieval Function....323
Implementing Cart-Manipulation Functions....324
Creating the Empty Cart Template....326
Finalizing the Front Controller....327
Adding Display Functions....328
Writing the switch Statement....328
Summary....330
Exercises....331
16. Authentication and Authorization....332
A Simple Login Form....333
Creating a Site with a Login Form....334
Defining the File Structure....335
Creating the Shared Page Content....335
Designing the Page Templates....338
Developing the Login Form....340
Writing the Front Controller....342
Implementing the Logic Functions....343
Creating the Error Page Template....345
Storing Login Data with Sessions....347
Updating the Front Controller....347
Writing the Login Function....348
Updating the Header Template....349
Updating the Banking Page Template....350
Offering a Logout Feature....350
Adding the Logout Function....351
Updating the Front Controller....351
Displaying the Logout Link....351
Displaying the Logged-in Username....352
Retrieving the Username....353
Updating the Navigation Bar....353
Updating the CSS....353
Summary....354
Exercises....354
Part V: Object-Oriented PHP....356
17. Introduction to Object-Oriented Programming....358
Classes and Objects....359
Creating Relationships Between Objects....361
Encapsulation and Information Hiding....362
Superclasses, Inheritance, and Overriding....362
The Flow of Control for Object-Oriented Systems....364
An Example Class Declaration....365
Summary....366
Exercises....367
18. Declaring Classes and Creating Objects....368
Declaring a Class....368
Creating an Object....370
Private Properties with Public Accessor Methods....372
Getting and Setting Private Properties....375
Screening for Invalid Data....375
Overriding Default Class Behavior with Magic Methods....377
Initializing Values with a Constructor Method....377
Converting Objects to Strings....380
Object Variables as References....382
Handling Missing Objects....382
Custom Methods and Virtual Attributes....384
Summary....385
Exercises....386
19. Inheritance....388
Inheritance as Generalization....389
Creating Objects from Subclasses....390
Using Multiple Levels of Inheritance....392
Protected Visibility....393
Abstract Classes....398
Overriding Inherited Methods....399
Augmenting Inherited Behavior....402
Preventing Subclassing and Overriding....405
Declaring a Class final....405
Declaring a Method final....406
Summary....408
Exercises....408
20. Managing Classes and Namespaces with Composer....412
Namespaces....413
Declaring a Classs Namespace....413
Using a Namespaced Class....414
Referencing Namespaces in Class Declarations....415
Composer....417
Installing and Testing Composer....417
Creating the composer.json Configuration File....417
Creating an Autoloader....419
Adding Third-Party Libraries to a Project....421
Where to Find PHP Libraries....423
Summary....424
Exercises....424
21. Efficient Template Design with Twig....426
The Twig Templating Library....427
How Twig Works....428
A Simple Example....429
Manipulating Objects and Arrays in Twig Templates....434
Twig Control Structures....437
Creating a Multipage Website with Twig....439
The File Structure and Dependencies....439
The Application Class....440
The Twig Templates....442
Twig Features to Improve Efficiency....444
Improved Page Styling with CSS....450
Summary....455
Exercises....456
22. Structuring an Object-Oriented Web Application....458
Separating Display and Front-Controller Logic....459
Using Multiple Controller Classes....461
Sharing Controller Features Through Inheritance....466
Summary....469
Exercises....469
23. Error Handling with Exceptions....472
The Basics of Exceptions....473
Throwing an Exception....473
Catching an Exception....477
Ending with a finally Statement....478
Using Multiple Exception Classes....480
Other Built-in Exception Classes....480
Custom Exception Classes....482
Call-Stack Bubbling....485
Summary....487
Exercises....487
24. Logging Events, Messages, and Transactions....490
Built-in PHP Resources for Logging....491
Predefined Constants for Severity Levels....491
Logging Functions....493
The Monolog Logging Library....495
Organizing Logs with Channels....496
Managing Logs According to Severity....497
Logging Exceptions....500
Logging to the Cloud....503
Summary....505
Exercises....505
25. Static Methods, Properties, and Enumerations....506
Storing Class-Wide Information....506
Static Properties vs. Class Constants....511
Utility Classes with Static Members....513
Sharing Resources Across an Application....516
Saving Resources with the Singleton Pattern....519
Enumerations....522
Backed Enums....525
An Array of All Cases....525
Summary....526
Exercises....526
26. Abstract Methods, Interfaces, and Traits....528
From Inheritance to Interfaces....529
Inheriting a Fully Implemented Method from a Superclass....529
Inheriting an Abstract Method....531
Requiring Method Implementations with Interfaces....533
Real-World Applications of Interfaces....540
Caching Approach 1: Using an Array....541
Caching Approach 2: Using a JSON File....545
Caching Approach 3: Creating a Cacheable Interface....547
Traits....552
Declaring Traits....553
Inserting Traits....555
Resolving Trait Conflicts....556
What to Use When?....556
Summary....558
Exercises....558
Part VI: Database-Driven Application Development....560
27. Introduction to Databases....562
Relational Database Basics....563
Database Management Systems....564
Structured Query Language....565
Databases and Web Application Architecture....566
Object-Oriented Programming....566
The Model-View-Controller Pattern....569
Summary....570
Exercises....570
28. Database Programming with the PDO Library....572
The PDO Library....573
A Simple Database-Driven Web Application....574
Setting Up the Database Schema....574
Writing the PHP Classes....578
Switching from MySQL to SQLite....582
A Multipage Database-Driven Web Application....584
Managing the Product Information....586
Implementing the Controller Logic....589
Designing the Templates....592
Summary....597
Exercises....597
29. Programming Crud Operations....600
Deleting Data....601
Deleting Everything from a Table....601
Deleting Individual Items by ID....604
Creating New Database Entries....608
Adding Products Through a Web Form....609
Highlighting the Newly Created Product....613
Updating a Database Entry....616
Avoiding Double Form Submission with Redirects....621
Summary....624
Exercises....625
30. Orm Libraries and Database Security....626
Simplifying Database Code with an ORM Library....627
Adding an ORM Library to a Project....629
Moving Database Credentials to a .env File....629
Relegating Product Operations to the ORM Library....630
Adding a New Database Table....632
Security Best Practices....639
Storing Hashed Passwords....639
Verifying Hashed Passwords at Login....640
Securing Database Credentials....646
The Doctrine ORM Library....646
Removing the Previous ORM Library....646
Adding Doctrine....647
Verifying That Doctrine Is Working....648
Creating Database Tables....649
Adding Records to a Table....652
Integrating Doctrine into the Application Code....653
Creating Foreign-Key Relationships....655
Summary....660
Exercises....660
31. Working with Dates and Times....662
The ISO 8601 Standard....663
Creating Dates and Times....664
Formatting the Date-Time Information....665
Using DateTimeImmutable vs. DateTime....666
Manipulating Dates and Times....668
Using Date-Time Intervals....670
Looping at Regular Intervals....671
Time Zones....672
Daylight Saving Time....675
Epochs and Unix Time....677
Date-Time Information in a Web Application....678
The Application Class....679
The Supporting Classes....681
The Templates....684
MySQL Dates....686
Summary....692
Exercises....693
A. Installing PHP....694
macOS....694
Linux....695
Windows....696
AMP Installations....698
B. Database Setup....700
MySQL....700
macOS and Windows....700
Linux....701
SQLite....702
Confirming the MySQL and SQLite Extensions....702
C. Replit Configuration....704
Changing the PHP Version....705
Adding the Composer Tool....706
Using the SQLite Database System....706
Serving Pages from the public Directory....707
Index....710
Back Cover....730
Tired of cobbling together PHP solutions from scattered online tutorials? Frustrated by outdated PHP practices that leave your code vulnerable and hard to maintain? Whether you’re building your first dynamic website or modernizing legacy systems, PHP Crash Course gives you a complete, practical foundation for writing professional web applications.
Starting with PHP fundamentals, you’ll progress through six carefully crafted sections covering essential patterns, security best practices, database integration, and advanced concepts like object-oriented programming. Each chapter builds on real-world examples, giving you the skills to solve common development challenges.
Whether you’re a complete beginner or an experienced developer looking to modernize your PHP skills, PHP Crash Course gives you everything you need to build professional, dynamic websites with confidence.
Includes setup instructions for Windows, macOS, and Linux, and configuration details for Replit, the free online development environment.