Table of Contents....5
About the Author....10
About the Technical Reviewer....11
Acknowledgments....12
Introduction....13
Chapter 1: The First Program....26
The Programs Code....26
The Interpreter Regime....28
The Server Regime....43
Summary....50
Chapter 2: Variables and Data Types....52
Introduction to Variables....52
Basic Data Types....56
The Arithmetic Operations....59
The Comparison Operations....62
The Logical Operations....64
The Bitwise Operations....66
The Operations with Strings....71
Assigning Values....73
The Ternary Operator....74
Summary....77
Chapter 3: The Control Statements....78
The if Conditional Statement....78
The while Loop Statement....87
The do-while Loop Statement....91
The for Loop Statement....94
The switch Selection Statement....99
The goto Instruction....107
The match Selection Statement....109
Summary....114
Chapter 4: Arrays....116
Getting Familiar with Arrays....117
A Loop over an Array....124
Multidimensional Arrays....132
Array Assignments....136
Concatenating Arrays....140
Comparing Arrays....142
Functions for Handling Arrays....144
Summary....150
Chapter 5: Functions....151
Creating Functions....151
The Function Result....156
The Type of Arguments and Result....161
The Argument Passing Mechanism....165
The Argument Value by Default....169
An Arbitrary Number of Arguments....171
Recursion....180
The eval() Function....183
Anonymous Functions....186
Named Arguments....188
Summary....193
Chapter 6: Useful Tricks and Operations....195
References....195
Constants....201
Global Variables....203
Static Variables....206
Multiline Strings....208
Using Files....211
Including a File in the Program....218
Summary....220
Chapter 7: Classes and Objects....222
The OOP Principles....222
Creating Classes and Objects....225
The Methods....230
The Constructor and the Destructor....234
Static Fields and Methods....238
Copying Objects....240
Private Fields and Methods....243
Special Methods....246
Defining Fields in the Constructor....258
Summary....262
Chapter 8: Inheritance....265
Creating a Child Class....265
Overriding Methods....270
Constructors and Inheritance....275
Inheritance and Private Members....279
Protected Members of a Class....281
Virtual Methods....283
A Function as a Field Value....285
Multilevel Inheritance....288
Summary....293
Chapter 9: Advanced OOP Mechanisms....295
Abstract Classes....295
Interfaces....300
Interface Inheritance....303
Class Inheritance and Interface Implementation....305
Traits....307
Object Type Control....310
A Namespace....314
Anonymous Classes....320
Summary....322
Chapter 10: Error Handling....325
Exception Handling Principles....325
Exception Classes....329
Throwing Exceptions....332
The Custom Exceptions....336
Handling Exceptions of Different Classes....340
Nested try-catch Constructions and the finally Block....346
Rethrowing an Exception....350
The Functions for Handling Errors....352
Summary....355
Chapter 11: Generators and Iterators....356
Getting Familiar with Generators....356
A Generator Function with Arguments....360
An Array Based on a Generator....362
The Generator Result....364
Using Generators....367
Passing a Value to a Generator....372
Iterators....374
Summary....380
Chapter 12: Using PHP....382
A Script in an HTML Document....382
Handling the Request Parameters....390
Using Buttons....394
Using Several Buttons....402
Using Lists and Checkboxes....407
A Slider and Radio Buttons....415
Summary....423
Chapter 13: Afterword: What Was and What Will Be....425
Index....427
Learn to create web applications in PHP with minimal previous experience. This book is a practical guide to using PHP for web development.
Loaded with examples and step-by-step instructions, each chapter is dedicated to a specific area or function. You’ll first review the main principles of PHP and what is needed to program and develop in it. You’ll then study variables, data types, control statements, arrays, and functions, all critical for creating efficient PHP programs.
The book then moves on to object-oriented programming (OOP) and how to implement those principles in PHP, as well as inheritance, interfaces, testing, error handling, and exceptions. By the end of PHP by Example, you will have the knowledge and confidence to implement PHP for your web projects both large and small.