PHP
Completed
-
Section 1: Getting Started
-
Section 2: Core Syntax
-
Section 3: Functions
-
Section 4: Object-Oriented PHP
-
Section 5: Working with Data
-
Section 6: Modern PHP (PHP 8)
-
Section 7: Working with Files and Networking
-
Section 8: Common PHP Frameworks Overview
-
Section 9: Tooling and Ecosystem
-
Section 10: Practical Projects
-
Section 11: Interview Practice
-
Section 12: More Standard Library
-
Section 13: Data Structures and Algorithms in PHP
-
Section 14: More Practice Exercises
-
Section 15: More Security and Best Practices
-
Section 16: More Testing and Tooling
-
Section 17: WordPress-Style CMS Concepts
-
Section 18: Advanced OOP Practice
-
Section 19: More Web Fundamentals
-
Section 20: Database Practice
-
Section 21: PHP Manual: Array Functions
-
Section 22: PHP Manual: Date and Calendar Functions
-
Section 23: PHP Manual: Filesystem and Directory Functions
-
Section 24: PHP Manual: Filter and Var Handling
-
Section 25: PHP Manual: Math Functions
-
Section 26: PHP Manual: JSON and XML
-
Section 27: PHP Manual: Network and Stream Functions
-
Section 28: PHP Manual: Error and Exception Handling
-
Section 29: PHP Manual: Output Control and Misc
-
Section 30: PHP Manual: FTP, Zip, and Mail
-
Section 31: Modern PHP Frameworks Deep Dive
-
Section 32: PHP Design Patterns
-
Section 33: More Practice Exercises
-
Section 34: PHP Performance and Deployment
-
Section 35: More Interview Practice
-
Section 36: More PHP Standard Library
-
Section 37: PHP Concurrency and Async
-
Section 38: More Web Development Practice
-
Section 39: PHP Testing Deep Dive
-
Section 40: Composer and Package Development
-
Section 41: PHP Security Deep Dive
-
Section 42: More Practical Projects
-
Section 43: Legacy PHP Maintenance
-
Section 44: More Algorithm Practice
-
Section 45: Final Practice and Review
-
Section 46: PHP for E-Commerce Patterns
-
Section 47: PHP API Design Deep Dive
-
Section 48: PHP Caching Strategies
-
Section 49: PHP Queue and Background Jobs
-
Section 50: PHP Multi-Tenancy Patterns
-
Section 51: PHP Real-Time Features
-
Section 52: PHP CMS and Content Modeling
-
Section 53: PHP Internationalization
-
Section 54: More Framework-Specific Practice
-
Section 55: PHP Legacy Code Refactoring
-
Section 56: More Practice Projects Round 2
-
Section 57: PHP Command-Line Applications
-
Section 58: PHP and Microservices
-
Section 59: More Interview and Review Round 2
180: Upgrading PHP 7 Code to PHP 8
I've got a legacy project on my desk—a user permission system written back in the PHP 7.2 era—and I'm tasked with moving it to PHP 8.2. On paper, it looks fine. No deprecated functions are screaming at me in the logs yet. But as any seasoned dev knows, the "silent" changes are the ones that actually break your production environment.
The Comparison Trap
I'm looking at a piece of logic that determines if a user has a "guest" status. In PHP 7, the developer wrote this:
$status = "guest_user";
if ($status == 0) {
echo "This is a guest!";
}
When I run this on my PHP 7.4 environment, it actually prints "This is a guest!". Why? Because PHP 7's loose comparison (==) would cast the string "guest_user" to an integer to compare it to 0. Since the string doesn't start with a number, it becomes 0. 0 equals 0. Logic passed. Weird, but that's how it worked.
Now, I flip the switch to PHP 8.2 and run it again. Silence. Nothing prints. I'm staring at the screen wondering why the logic suddenly failed. I check the docs, and there it is: PHP 8 changed how numeric strings are compared. Now, if you compare a number to a non-numeric string, PHP converts the number to a string and compares them. "0" does not equal "guest_user".
This is a huge deal. If your codebase relies on these "lucky" loose comparisons, your app is going to start behaving randomly after an upgrade. The fix is simple, but tedious: I have to go through and ensure I'm using strict comparisons (===) or actually checking the type. I'll change it to if ($status === 'guest') and sleep better tonight.
Taming the Switch
While I'm digging through the UserRole class, I find this monstrosity of a switch statement that maps roles to permission levels. It's about 40 lines of case, break, and a default.
switch ($role) {
case 'admin':
$level = 10;
break;
case 'editor':
$level = 5;
break;
case 'user':
$level = 1;
break;
default:
$level = 0;
break;
}
It works, but it's noisy. Since I'm upgrading to PHP 8, I remember the match expression. I'm going to try replacing this whole block to see if I can make it more readable. I'll try this:
$level = match ($role) {
'admin' => 10,
'editor' => 5,
'user' => 1,
default => 0,
};
I refresh the page. It works perfectly. Not only is it shorter, but match is strict. If I accidentally passed an integer 1 instead of a string '1', match wouldn't just guess; it would throw an error if no match was found. It's a safety net that PHP 7 just didn't provide.
Shrinking the Boilerplate
Finally, I'm looking at the Data Transfer Objects (DTOs) used for user profiles. They are full of the same repetitive pattern: declare the property, then assign it in the constructor. It's boring to write and boring to read.
class UserProfile {
public string $username;
public string $email;
public function __construct(string $username, string $email) {
$this->username = $username;
$this->email = $email;
}
}
In PHP 8, we have Constructor Property Promotion. I'm going to try and collapse this. I'll move the visibility modifiers (public) directly into the constructor arguments.
class UserProfile {
public function __construct(
public string $username,
public string $email,
) {}
}
I just deleted five lines of code and the class does exactly the same thing. PHP automatically declares the properties and assigns the values for me. It feels like cheating, but in a large project with hundreds of these classes, this cleanup removes an incredible amount of visual noise.
📋 Practical Task
Refactoring the Legacy Order Status Handler
You have been handed a legacy file from a PHP 7.4 project. Your goal is to upgrade it to PHP 8.2 standards by fixing a logic bug and modernizing the syntax.
The Code:
class OrderHandler {
public $status;
public $orderId;
public function __construct($status, $orderId) {
$this->status = $status;
$this->orderId = $orderId;
}
public function getStatusLabel($status) {
switch ($status) {
case 'pending':
return 'Waiting for payment';
case 'shipped':
return 'On the way';
case 'delivered':
return 'Arrived';
default:
return 'Unknown';
}
}
public function isInvalid($status) {
// This was intended to return true if status is 0 or "0"
// But in PHP 7, "pending" == 0 was true.
return $status == 0;
}
}
$order = new OrderHandler("pending", 101);
echo $order->isInvalid("pending"); // In PHP 7, this printed 1. In PHP 8, it prints nothing/false.
Your Tasks:
- Fix the Bug: Rewrite the
isInvalidmethod so it explicitly checks if the status is the string "0" or the integer 0, without relying on loose comparison traps. - Modernize the Logic: Replace the
switchstatement ingetStatusLabelwith a PHP 8matchexpression. - Clean the Boilerplate: Use Constructor Property Promotion to rewrite the
OrderHandlerclass, adding type hints (stringfor status,intfor orderId).
There are no comments for now.