Sharpening My PHP Skills for Modern PHP 8
An interviewer wanted experience with the latest PHP, not just PHP. So I rebuilt the fundamentals from scratch in a small task manager, using PHP 8 the way it's meant to be written today.
PHP isn't new to me. I've used it in past projects, and I've written my share of forms, database queries, and login pages with it.
But in a recent interview, the interviewer was clear about what they needed: someone with experience in modern PHP. Not just "has used PHP," but someone comfortable with how the language looks and works today.
That was fair feedback. PHP has changed a lot over the last few versions, and most of my PHP experience came from projects that didn't use those newer features. So rather than skimming a changelog, I decided to sharpen my skills properly: rebuild a small, complete app from scratch, using current PHP 8 features and current best practices throughout.
This post covers what I built, what's changed in the language, and what I'm working on next.
The Plan: One Small App, Written the Modern Way
I built Tasker, a task manager with user accounts. It's deliberately small, but it covers everything a real web app needs:
- Registration, login, and logout
- Tasks with a priority and an optional due date
- Create, edit, complete, and delete tasks
- Filtering (all / open / done) and search
- Overdue highlighting
- Each user only sees their own data
I wrote it in plain PHP 8.4 with no framework. That was a deliberate choice. Frameworks are great at hiding routing, sessions, validation, and security behind helpful abstractions, but I wanted to refresh my understanding of what's happening underneath first. That's exactly the knowledge that comes up when an interviewer asks, "How does CSRF protection actually work?"
The stack is intentionally minimal:
- PHP 8.4 with
declare(strict_types=1)in every file - PDO with SQLite, so the database is a single file
- PHP's built-in web server for development
php -S localhost:8000 -t public public/index.php
What's Changed: Old Habits vs Modern PHP
The biggest value of this exercise was replacing old habits with modern equivalents. Here's a summary of the shifts I focused on:
| Older style | Modern PHP |
|---|---|
array(1, 2, 3) | [1, 2, 3] |
isset($x) ? $x : 'default' | $x ?? 'default' |
Long switch blocks | match expressions |
| Untyped functions | Typed parameters and returns, strict_types |
function ($t) { return $t['done']; } | fn ($t) => $t['done'] |
| No "this never returns" type | never return type |
mysql_* functions, string-built SQL | PDO with prepared statements |
md5() or sha1() for passwords | password_hash() and password_verify() |
| Silent failures | Exceptions (PDO::ERRMODE_EXCEPTION) |
None of these are exotic. But together they make PHP code read very differently, and they're the first things an experienced reviewer notices.
Strict Types and Real Signatures
Every file in Tasker starts with:
declare(strict_types=1);
Without it, PHP quietly converts types for you, so passing "5" where an int is expected just works. With strict types on, it throws a TypeError instead. That catches bugs early, and it makes function signatures something you can actually trust:
function e(?string $value): string
{
return htmlspecialchars($value ?? '', ENT_QUOTES, 'UTF-8');
}
The ?string means "string or null," and ?? handles the null case in one expression.
match and never: A Router in a Few Lines
Every request goes through one front controller, public/index.php. PHP 8's match expression made the router far cleaner than the switch statements I used to write:
match (true) {
$path === '/login' && $method === 'GET' => view('login'),
$path === '/login' && $method === 'POST' => Auth::handleLogin(),
$path === '/tasks' && $method === 'GET' => Task::index(),
$path === '/tasks' && $method === 'POST' => Task::store(),
(bool) preg_match('#^/tasks/(\d+)/delete$#', $path, $m) && $method === 'POST'
=> Task::delete((int) $m[1]),
default => abort(404),
};
Unlike switch, match uses strict comparison, has no fall-through, and throws an error if nothing matches and there's no default.
Functions that always redirect or stop execution use the never return type, added in PHP 8.1:
function redirect(string $to): never
{
header("Location: {$to}");
exit;
}
It documents intent, and static analysis tools can warn you if code after a redirect() call could never run.
Database Access: PDO, Exceptions, and Prepared Statements
PDO isn't new, but using it well is the baseline for modern PHP:
$pdo = new PDO('sqlite:' . __DIR__ . '/../data/app.sqlite');
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$pdo->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_ASSOC);
Errors throw exceptions instead of failing silently, and every query that touches user input uses a prepared statement:
// ❌ SQL injection risk
$pdo->query("SELECT * FROM users WHERE email = '$email'");
// ✅ The value is sent separately from the SQL
$stmt = $pdo->prepare('SELECT * FROM users WHERE email = ?');
$stmt->execute([$email]);
Because the code only uses PDO, switching from SQLite to MySQL or PostgreSQL is mostly a matter of changing the connection string.
Security, Done Properly
Rewriting the security layer by hand was the most useful part of the refresh. These are the areas I made sure to get right.
Passwords
$hash = password_hash($password, PASSWORD_DEFAULT);
if (!$user || !password_verify($password, $user['password_hash'])) {
flash('Invalid email or password.', 'error');
redirect('/login');
}
PASSWORD_DEFAULT uses a strong, salted algorithm and lets PHP upgrade it in future versions. The login error is identical for "unknown email" and "wrong password," so it doesn't reveal which accounts exist. After login, session_regenerate_id(true) issues a fresh session ID to prevent session fixation.
XSS
Every value printed into HTML goes through the e() helper above. I tested it by creating a task titled <script>alert('hi')</script>, and it renders as harmless text.
CSRF
Every form includes a random token stored in the session, and every POST request is checked before any route runs:
function verify_csrf(): void
{
if (!hash_equals(csrf_token(), $_POST['_csrf'] ?? '')) {
abort(419);
}
}
hash_equals() compares in constant time, so the check can't leak information through response timing.
Access Control
Being logged in isn't enough. Every task query checks ownership, so changing /tasks/42/edit to /tasks/43/edit can't expose another user's data:
private static function findOwned(int $id): array
{
$stmt = db()->prepare('SELECT * FROM tasks WHERE id = ? AND user_id = ?');
$stmt->execute([$id, $_SESSION['user_id']]);
return $stmt->fetch() ?: abort(404);
}
A Reminder That PHP Is Still PHP
Not everything has changed. In a PHP file, ?> ends PHP mode even inside a // comment. I wrote a comment in a template that mentioned the <?= ?> syntax, and it would have silently switched the rest of the file back to plain HTML.
Modern PHP is far more strict and expressive, but it's still a language embedded inside text files, and some of its old quirks are still there.
Next: The Features Tasker Doesn't Use Yet
Tasker covers the fundamentals, but there's more of PHP 8 I want to put into practice. My next step is refactoring it to use:
| Feature | Version | Where it fits in Tasker |
|---|---|---|
| Enums | 8.1 | Replace the PRIORITIES string array with enum Priority: string |
| Constructor property promotion | 8.0 | Turn task rows from arrays into small classes |
| Readonly properties and classes | 8.1 / 8.2 | Make those task objects immutable |
| Named arguments | 8.0 | Clearer calls like htmlspecialchars($v, double_encode: false) |
Nullsafe operator ?-> | 8.0 | Safely chain calls on optional objects |
| First-class callable syntax | 8.1 | array_map($this->format(...), $tasks) |
| Property hooks | 8.4 | Computed properties like $task->isOverdue |
| Asymmetric visibility | 8.4 | public private(set) for read-only-from-outside fields |
After that, I'll add Composer, PHPUnit tests, and a static analyser like PHPStan, and then rebuild the same app in the latest Laravel. That's the stack most modern PHP teams use day to day, and having written each piece by hand first will make it much easier to explain why Laravel does things the way it does.
Wrapping Up
That interview feedback stung a little, but it was useful. Having used a language before and knowing its current version well are two different things, and companies hiring PHP developers today care about the second one.
If you're in the same position, coming back to a language you've used before, my advice is to build one small but complete project from scratch with the latest version. It shows you exactly which habits are out of date, and it gives you real code to point to in your next interview.
Thanks for reading! If you're also brushing up on modern PHP, I'd love to hear what you're building.
Written by
Niluka Bandara
Lead Mobile Developer in Stockholm, building Flutter apps for fintech and payments.