Originally published on decentphp.dev — part of the Writing decent PHP course. View this lesson on the original site.
Why this course exists
Most PHP courses stop at "you can build it."
This course continues to "you can maintain it, change it safely, and hand it over without drama."
Course standard: "It works" is the starting line, not the finish line.
That means we focus on code that:
- reduces avoidable regressions during normal feature work
- makes reviews faster and less subjective
- lowers onboarding and handover friction in real teams
- chooses simple, clear solutions over clever over-engineering
You do not need to fully understand everything yet.
We will build these ideas step by step as the course progresses.
The reality we are trying to avoid
You open the codebase.
It is PHP 5.5.9.
No tests.
One file has 5,000 lines and reads like it was written under the heavy influence of a psychedelic drug.
Comments like:
- "don't delete this, it breaks stuff"
- "not sure why this works, but don't touch it"
Yep, this is not a joke. This has unfortunately happened to me several times.
This course is designed to help prevent exactly this kind of codebase from happening.

Goal: leave code better than you found it - clearer, safer, and easier to change.
Let's look at a quick example of the same function in two shapes: first nested, then flattened.
Before
Nested if blocks make the control flow harder to scan because you have to keep multiple conditions in your head at once.
<?php
function sendReceiptIfNeeded(array $order, $mailer): void
{
if (($order['is_paid'] ?? false) === true) {
if (($order['customer_email'] ?? '') !== '') {
$mailer->send($order['customer_email'], 'Your receipt');
}
}
}
After
Now keep the behaviour the same, but rewrite the flow so each rule exits early.
Early returns help you flatten nested conditionals so the happy path is easier to read.
<?php
function sendReceiptIfNeeded(array $order, $mailer): void
{
if (($order['is_paid'] ?? false) !== true) {
return;
}
$email = (string) ($order['customer_email'] ?? '');
if ($email === '') {
return;
}
$mailer->send($email, 'Your receipt');
}
Why this version is better
- the happy path is obvious because guard clauses remove nesting
- each rule is checked once, top-to-bottom, without mental stack juggling
- behaviour is unchanged, but the function is easier to review and maintain
This is exactly what this course is about: writing PHP that is decent.
Decent code is readable and maintainable, not just code that happens to work today.
From Writing decent PHP — What decent PHP looks like