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
7: Control Flow Statements
I'm seeing if, switch, and this newer match expression. Which one should I actually be using?
This is a classic point of confusion because PHP has evolved quite a bit. Here is how I decide which one to reach for. Use if/elseif/else when you're dealing with complex logic, ranges (like $age > 18 && $age < 65), or conditions that aren't based on a single value.
Now, if you're just checking one variable against a list of specific values, switch used to be the go-to. But in modern PHP (8.0+), you should almost always use match instead. Why? Because match is an expression—it actually returns a value—and it uses strict comparison (===), which saves you from some really annoying bugs where the string "0" might accidentally match an integer 0.
// The old way (switch)
switch ($userRole) {
case 'admin':
$access = 'full';
break;
case 'editor':
$access = 'limited';
break;
default:
$access = 'none';
}
// The modern way (match)
$access = match ($userRole) {
'admin' => 'full',
'editor' => 'limited',
default => 'none',
};
I personally find match much cleaner. No more forgetting a break; statement and accidentally falling through into the next case.
Why is everyone telling me to use foreach instead of a regular for loop?
In other languages, you're used to the for ($i = 0; $i < count($arr); $i++) pattern. In PHP, that's mostly a waste of your time unless you specifically need the index number for some mathematical reason. Since PHP arrays are actually ordered maps, they don't always have sequential numeric keys.
foreach is just more honest about what you're doing: you're visiting every item in a collection. It's cleaner, and you can't accidentally create an infinite loop or go "out of bounds."
$cart = [
'Laptop' => 1200,
'Mouse' => 25,
'Keyboard' => 75
];
foreach ($cart as $item => $price) {
echo "The $item costs $$price\n";
}
See how I grabbed both the key (the product name) and the value (the price) in one line? Trying to do that with a standard for loop in PHP is a nightmare.
What's the deal with "truthy" values? Do I always need to use === true?
You'll see a lot of PHP code that just says if ($variable) instead of if ($variable === true). This is because PHP has a concept of "truthiness." Basically, PHP will try to cast whatever you put in there to a boolean.
Most things are true, but there are a few "falsy" values you have to memorize: false, 0, 0.0, an empty string "", null, and an empty array []. I'll admit, this can be dangerous. If you're checking if a user entered "0" in a form, if (!$input) will trigger because 0 is falsy, even though the user actually provided a value.
My rule of thumb: if you're checking a boolean flag, if ($isActive) is fine. If you're checking data that could be a number or a string, always use strict comparison (===) to avoid those weird PHP type-juggling surprises.
📋 Practical Task
Build a Tiered Discount Calculator
You need to write a script that calculates the final price of a shopping cart based on a user's membership tier. Use the following requirements:
- Create an associative array called
$cartwhere keys are item names and values are prices. - Create a variable
$userTierwhich can be 'Gold', 'Silver', 'Bronze', or 'Guest'. - Use a
matchexpression to determine the discount percentage:- Gold: 20%
- Silver: 10%
- Bronze: 5%
- Guest: 0%
- Use a
foreachloop to sum up the total of all items in the cart. - Calculate the final total after applying the discount and print a message like: "Your total after the Gold discount is $X.XX".
There are no comments for now.