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
150: Timed Mock Assessment on Core PHP
You've hit the home stretch of the core PHP modules. Now, we need to make sure you can actually synthesize everything—arrays, loops, functions, and logic—under a bit of pressure. In a real technical interview or a certification exam, you aren't just asked "what is a foreach loop?"; you're asked to solve a problem using one. That's why this lesson is a mock assessment. Instead of just giving you a test, I'm going to show you how I'd tackle a common "timed" challenge: building a basic Stock Inventory Analyzer.
Setting the clock and the scope
If I had 30 minutes to build a tool that calculates total inventory value and flags low-stock items, I wouldn't spend ten minutes planning a database schema. I'd start with the data. For a mock assessment, I want a structure that's flexible but simple. I'll use a multidimensional associative array. It's the bread and butter of PHP data handling.
$inventory = [
["name" => "Mechanical Keyboard", "price" => 120.00, "quantity" => 15],
["name" => "Gaming Mouse", "price" => 60.00, "quantity" => 3],
["name" => "UltraWide Monitor", "price" => 450.00, "quantity" => 8],
["name" => "USB-C Cable", "price" => 15.00, "quantity" => 50],
];
Wrangling the totals with a loop
The first requirement is usually a summary. I need the total value of all stock. I'll initialize a accumulator variable and loop through the array. I'm choosing a foreach here because it's cleaner than a for loop when dealing with indexed arrays of associative arrays.
$totalValue = 0;
$lowStockItems = [];
foreach ($inventory as $item) {
$totalValue += $item['price'] * $item['quantity'];
if ($item['quantity'] < 10) {
$lowStockItems[] = $item['name'];
}
}
echo "Total Inventory Value: $" . number_format($totalValue, 2);
The case-sensitivity trap
Now, let's say the assessment asks for a function to find a specific product. I'll write a quick helper function. Here is where I usually trip up when I'm rushing—I forget that string comparisons in PHP are case-sensitive.
function findProduct($inventory, $searchName) {
foreach ($inventory as $item) {
if ($item['name'] == $searchName) { // This is the mistake
return $item;
}
}
return null;
}
// Testing it out
$result = findProduct($inventory, "gaming mouse");
// Result: null. Wait, what?
I just spent two minutes staring at the screen wondering why "gaming mouse" didn't match "Gaming Mouse". It's a classic "timed test" brain-fart. In a real scenario, you can't guarantee the user (or the test data) will match your casing perfectly. I need to normalize both strings using strtolower().
Polishing the search logic
I'll fix the function and add a check to handle the null return so the script doesn't crash when it can't find an item. This kind of "defensive programming" is what separates a junior dev from a professional—it shows you're thinking about edge cases.
function findProduct($inventory, $searchName) {
foreach ($inventory as $item) {
if (strtolower($item['name']) === strtolower($searchName)) {
return $item;
}
}
return null;
}
$product = findProduct($inventory, "gaming mouse");
if ($product) {
echo "Found it! Price: $" . $product['price'];
} else {
echo "Product not found.";
}
By building this, we've covered data structures, arithmetic, conditional logic, array appending, and string manipulation. That's the core of PHP. When you move into the exercise below, don't just make the code work—make it robust.
📋 Practical Task
Build a Customer Loyalty Tier Classifier
Create a PHP script that processes a list of customers and assigns them a loyalty tier based on their total spending. Your script must meet the following requirements:
- Data: Create an associative array called
$customerswhere each key is a customer name and the value is their total spend (e.g.,"Alice" => 1200.50). You should have at least 5 customers. - Logic: Write a function called
assignTier($spend)that returns:- "Platinum" for spend over $2,000
- "Gold" for spend between $1,000 and $2,000
- "Silver" for spend between $500 and $999
- "Bronze" for anything below $500
- Output: Use a loop to iterate through the customers and print a sentence for each: "[Name] has spent $[Amount] and is in the [Tier] tier."
- Bonus: Calculate and print the average spend of all customers at the bottom of the list.
There are no comments for now.