Skip to Content
Course content

150: Timed Mock Assessment on Core PHP

Click on the "Edit" button in the top corner of the screen to edit your slide content.

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 $customers where 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.
Rating
0 0

There are no comments for now.

to be the first to leave a comment.