Skip to content
Find bugIntermediate4 questions

Find the bug: everyday PHP

Four short snippets that look right and are not.

  1. 01This prints "not found" even though "apple" contains "a". Why?
    <?php
    
    if (strpos("apple", "a")) {
        echo "found";
    } else {
        echo "not found";
    }
  2. 02This prints 1, not 2. Why?
    <?php
    
    function addItem(array $items, string $item): void
    {
        $items[] = $item;
    }
    
    $cart = ["apple"];
    addItem($cart, "pear");
    
    echo count($cart);
  3. 03The developer expected 2025-02-28. What does this actually print?
    <?php
    
    echo date("Y-m-d", strtotime("+1 month", strtotime("2025-01-31")));
  4. 04Calling double("abc") here ends in an uncaught fatal error instead of printing "caught". Why?
    <?php
    
    function double(int $n): int
    {
        return $n * 2;
    }
    
    try {
        echo double("abc");
    } catch (Exception $e) {
        echo "caught";
    }

Answers are scored on the server. Unanswered questions count as incorrect.

laravel.now is an independent community project. It is not affiliated with, endorsed by, sponsored by, or in any way officially connected to Laravel, Laravel LLC, Taylor Otwell, or the Laravel framework maintainers. Laravel is a trademark of Taylor Otwell. All other trademarks belong to their respective owners.

This site is operated by the makers of Lockhound. Links to Lockhound are promotional.