PHP Essentials
PHP is one of those languages that is easy to start with and easy to keep using badly. The syntax is small enough to read in an afternoon, the standard library is large enough that almost every task has a reasonable answer somewhere inside it, and the language will quietly convert your types for you if you let it. The result is that a great deal of PHP in the wild looks like three frameworks stapled together with a deployment script, and that the gap between “I know programming” and “I can work in a PHP codebase” is wider than it should be. This course takes the opposite path. It starts from the syntax the language assumes, and it builds up the mental model that everything later depends on, one working step at a time, so that the code you write afterwards is a consequence of decisions you understand rather than a template you copied.
The course exists to solve a specific problem. The parts of PHP that beginners actually lose their way over are not the exotic ones. Very few people are confused by an array. They are confused by the fact that an array is simultaneously a list and a dictionary, and that the standard library treats it as both; by the fact that a function is a value that can be captured, passed and composed; by the fact that a class can hold state that changes under a readonly modifier and state that does not; and by the fact that the language has four different ways of relating two types and no single rule that tells you which to use. Those are learnable facts, they are checkable facts, and they are the facts this course is built around.
The material is in four parts, and the parts are meant to be read in order. The first part is the syntax: what the opening tags mean, what a statement is, how variables work and what it means that an assignment shares a value rather than copying it, the four kinds of string and what each of them substitutes, the type system and the difference between weak typing and the strictness declaration, the operator table and the two rows of it that surprise everybody, constants, the four scopes, the four output functions, and the difference between including a file and requiring one. The second part is arrays: the ordered-map model that explains every function in the library, the two shapes a single type can take, how to read a key safely, the imperative and the functional style side by side, and the exact list of functions that copy, that modify in place, and that preserve the keys. The third part is functions: how a signature states a contract, how parameters may be optional, by reference or variadic, how the language’s support for named arguments changes the way you design a public API, what closures and arrow functions are actually for, how a function becomes a value, and how the built-in library is organised so that you know where to look before you write a loop. The fourth part is the object model: properties with a deliberate visibility, state that is protected by readonly, the constructor forms, the difference between self and static, and the four ways of relating two types, followed by exceptions, the magic methods, and the decisions about cloning and serialisation that every long-lived object has to answer.
The order is not arbitrary, and each part assumes the previous one. Nothing about the object model can be evaluated before you know what a variable is and what a type is, because the words the object model is written in are property, parameter and return type. Nothing about functions can be judged before you know what a value is and how arrays behave, because a closure captures values from the surrounding scope and the array functions take callbacks. And nothing about the syntax can be skipped, because the strictness declaration, the nullable type and the readonly modifier are all syntax, and because the reference semantics that decide whether a function can modify its caller’s data are decided by the assignment rules. Reading the chapters out of order is possible, but each one will refer back to something that has not been established yet, and the references will feel arbitrary rather than natural.
The prerequisites are modest. You should already be comfortable with the basic syntax of some other programming language: variables, conditionals, loops, functions, and the idea of a data structure. You do not need any PHP at all, because the course starts with the syntax rather than with an installation guide. You should be willing to read a function signature as a contract, and to treat a type declaration as something the language will enforce rather than as a comment. It helps to have seen a functional style before, because the array chapter moves quickly through map, filter and reduce, but it is not required: every one of them is introduced with its own example. A reader who has never used a callback will lose a little time in the third chapter and gain it back in the fourth, where callbacks turn out to be the mechanism that traits, interfaces and the standard library all share.
Two conventions hold throughout. Every example targets PHP 8.x, and where a feature depends on a particular point release, the release is named. Every code sample is meant to be run: the snippets are short enough to paste into a file and execute, and where a fragment cannot run on its own, the text says so and explains what it is a fragment of. The point of the convention is that a reader can always tell the difference between a claim about PHP and a claim about an example, and can check the second kind for themselves.
The course is deliberately narrow. It covers the language, not the ecosystem. There is no Composer walkthrough, no framework, no template engine, no database layer and no deployment material, because each of those is a large subject that assumes the material covered here. That is not a limitation of the material but the boundary of it, and the boundary is drawn where it is because the language is the part that every framework is written in, the array type is the part that most of that code is built on, and the object model is the part that a framework’s own concepts are modelled with. If you take the four chapters in order and finish the exercises at the end of each one, you will be able to read an ordinary PHP file and say what every declaration does, predict how a value will be converted when two types meet, choose the right array function for a job, and decide where the boundary between two classes belongs.
There is a way of working through this material that is much more effective than reading it. Keep a terminal open next to the text, and run every example as you meet it rather than assuming it will behave as described. The examples are short enough that running them costs little, and the failures are informative: a warning about an uninitialised typed property, a deprecation notice about a lossy float conversion, and an error about an unknown named parameter are all part of the lesson. Writing the examples out by hand rather than copying them is slower and worth it, because the mistakes you make while typing are the ones you will remember the fix for.
It is also worth saying plainly who this course is not for. It is not a route to a framework: there is no Laravel, no Symfony and no WordPress development in it, because each of those assumes the material covered here and adds a great deal more. It is not a reference for the whole standard library either, and it is not a guide to performance, to concurrency, to the date and time extension, or to the internals of the engine. What it is for is the first stretch of the road, where the language is still unfamiliar and its opinions are still invisible, and where a reader who gets those two right will find everything that comes afterwards considerably easier to evaluate.
By the end of the course you will have a working understanding of PHP’s syntax and type rules, an instinct for the array type and the library that is built around it, a function model that lets you write and compose reusable behaviour, and an object model that lets you put boundaries around state and protect the invariants behind them. You will also have the habits that make the rest of the language learnable: read the declaration before the body, prefer the explicit conversion to the implicit one, verify the behaviour by running the example, and keep the state of an object behind methods that are allowed to protect it. Those habits are what separate someone who has written PHP from someone who can maintain PHP.
Each chapter closes with a worked reference answer that is written out in full: the reasoning that leads to the solution, the individual steps in the order they are executed, and the final result together with the evidence that shows the work succeeded. Those answers are meant to be read after you have attempted the exercise, because the value of a reference solution is largest when it can be compared against your own attempt, and the differences between the two are where the remaining misunderstanding usually is. They also work as a second reading of the chapter: the steps restate the material in an order that a beginner would not have guessed, which is often the clearest description of the reasoning the chapter was trying to teach.
Before you read on, write down your answer to this: you have just joined a team that maintains a PHP codebase of roughly forty thousand lines, and you have been asked to review a pull request that adds a method to a class, changes the key of an array that three other methods read, and introduces a use statement for a trait.
Which of the four chapters of this course do you need first, and in what order do you need them? For each one, name the specific fact you would be looking up, and say why the other three are not the place to start.
Approach:
The course is organised around a dependency chain rather than around a feature list. A reader cannot reason about a type declaration before knowing what a type is, cannot reason about an array function before knowing how an array is keyed, cannot reason about a closure before knowing what a variable captures, and cannot reason about a class before knowing what a function and a return value are. Each chapter therefore produces an artefact the next chapter consumes: a set of syntax rules with a type model, a data structure with a library, a unit of behaviour that can be composed, and an object system that puts a boundary around state. The teaching moves from the concrete and observable to the abstract, and every stage ends with something the reader can run rather than merely believe.
Steps:
- Establish the physical and syntactic ground truth first: what the opening tags do, what a statement and a comment are, how a variable is created and what it holds, how the four string forms differ in what they substitute, and what the type system does when two types meet.
- Take the type rules seriously at the same time, because they decide every later question: the difference between weak typing and
declare(strict_types=1), the rule that the calling file decides strictness, the conversion table, and the explicit casts that make a conversion visible in the code. - Introduce the operator table, the constants, the four scopes, the four output functions, file inclusion and the superglobals, so that a complete PHP file can be read from end to end.
- Then treat the array as what it actually is, an ordered map, and use that model to explain every function in the library: the two shapes, safe reads, the imperative loop against the functional pipeline, and the three questions to ask of any array function, which are whether it copies, whether it modifies in place, and whether it preserves the keys.
- Move to the function, where the contract moves into the signature: parameter types, defaults, nullable and union types, pass by reference, variadics, named arguments, and the return types including
voidandnever. - Explain why functions are values, because that is the mechanism behind the whole standard library: closures with explicit capture, arrow functions with automatic capture, variable functions, the first-class callable syntax, and
call_user_func. - Then the recursion rules and the structure of the built-in library, so that the reader knows where to look before writing a loop: strings, arrays, files, mathematics, and dates and times.
- Finish the procedural layer with the object model, where state and behaviour are given a shape: typed properties with a deliberate visibility,
readonlystate that cannot change, the constructor forms including promotion, and the magic constants. - Then the relationships between types, which is where the design decisions live:
selfagainststatic, inheritance,final, abstract classes, interfaces, traits, and type hints withinstanceof. - Close with the parts of the object system that interact with the rest of the program: exceptions and the order of the
catchblocks, the magic methods, the shallow copy thatcloneperforms, the safe use of serialisation, and the iterator interfaces that make a class behave like the collection it stands for.
Answer:
A reader who completes this course can answer four questions about any PHP program they open. What type is every value here, and when will it be converted? What does each array function do to the keys and to the original array? What does each function promise to accept and to return? And what does each class own, who may see it, and how is it related to the other types in the system?
The first question is answered by the syntax and the type rules: the declarations in the signatures, the strictness setting of the calling file, and the conversion table that applies when no declaration covers the case. The second is answered by the ordered-map model of the array and by the habit of asking whether a function copies, mutates or preserves. The third is answered by the signature and by the callback conventions, which turn a function into a value that the library can compose with. The fourth is answered by the declarations themselves: private properties behind validating methods, readonly state that cannot change, static in a factory so that a subclass is created correctly, extends and implements chosen deliberately, traits used only to share code, and exceptions caught from the most specific type to the most general.
Those four answers are what a working PHP developer uses every day, and they are the reason the course is ordered the way it is.
