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
31: Match Expressions
You've probably spent a lot of time using switch statements. They get the job done, but if you've ever forgotten a break and spent twenty minutes wondering why your code is executing five different blocks of logic, you'll appreciate why match was added to PHP 8.0. Think of it as a more modern, stricter, and more concise cousin of the switch.
How is this actually different from a switch statement?
The biggest difference is that match is an expression, not a statement. In plain English: match returns a value. You can assign the result directly to a variable or return it from a function without having to manually update a variable inside every single case.
Another critical detail is that match uses strict comparison (===), whereas switch uses loose comparison (==). This saves you from those weird PHP bugs where the integer 0 somehow matches a string "0" or a null value.
// The old switch way
switch ($paymentStatus) {
case 'paid':
$message = 'Thank you for your purchase!';
break;
case 'pending':
$message = 'Your payment is processing.';
break;
default:
$message = 'Payment failed.';
break;
}
// The match way
$message = match ($paymentStatus) {
'paid' => 'Thank you for your purchase!',
'pending' => 'Your payment is processing.',
default => 'Payment failed.',
};
Do I still need to use break?
Nope. Forget about break entirely. One of the most annoying things about switch is "fall-through," where the code keeps running into the next case if you forget that one word. match doesn't do that. Once it finds a match, it returns the value and stops immediately. It's much cleaner and significantly less prone to human error.
What happens if none of the arms match the value?
This is where you have to be careful. If match doesn't find a matching arm and you haven't provided a default, PHP will throw an UnhandledMatchError. This might seem annoying, but as an engineer, I actually love this. It forces you to be explicit about every possible state your data can be in, which means fewer "silent failures" in production.
I always recommend adding a default arm unless you are 100% certain the input is restricted to a specific set of values (like an Enum).
Can I handle multiple values for one result?
Yes, and it's way more elegant than stacking case statements on top of each other. You just separate the values with commas. It's essentially an "OR" operation.
$userRole = 'editor';
$accessLevel = match ($userRole) {
'admin', 'superadmin' => 10,
'editor', 'author' => 5,
'subscriber' => 1,
default => 0,
};
echo "Access level: $accessLevel"; // Outputs: Access level: 5📋 Practical Task
Build an HTTP Response Status Mapper
You are building a debugging tool that takes a numeric HTTP response code and converts it into a human-readable category. Create a script that uses a match expression to map the following codes:
200and201should return"Success"400,401, and403should return"Client Error"404should return"Not Found"500and503should return"Server Error"- Any other code should return
"Unknown Status"
Test your solution by assigning a code to a variable (e.g., $code = 404;) and printing the result of the match expression.
There are no comments for now.