Lesson 5 โ€ข Beginner

    Loops ๐Ÿ”

    Repeat code efficiently with for, while, do-while, and foreach โ€” PHP's most powerful and frequently used loop.

    What You'll Learn in This Lesson

    • โ€ข For loop for counted iterations
    • โ€ข While and do-while for condition-based loops
    • โ€ข Foreach for iterating arrays (PHP's most-used loop)
    • โ€ข Break and continue to control loop flow
    • โ€ข Nested loops and real-world order processing examples

    1๏ธโƒฃ Loop Types at a Glance

    LoopBest ForSyntax
    forKnown number of iterationsfor ($i=0; $i<10; $i++)
    whileUnknown iterations, check firstwhile ($condition)
    do-whileRun at least oncedo while ($cond)
    foreachIterate arrays (most common!)foreach ($arr as $v)

    Try It: Basic Loops

    Practice for, while, do-while, and foreach loops with real examples

    Try it Yourself ยป
    JavaScript
    // PHP Loops (simulated in JavaScript)
    console.log("=== For Loop ===");
    console.log();
    console.log("Syntax: for ($i = 0; $i < count; $i++) { ... }");
    console.log();
    
    // for loop โ€” when you know how many times to loop
    for (let i = 1; i <= 5; i++) {
        console.log("  Iteration " + i + ": " + "โญ".repeat(i));
    }
    
    console.log();
    console.log("=== While Loop ===");
    console.log();
    console.log("Syntax: while ($condition) { ... }");
    console.log("Use when you DON'T know how many iterations.");
    console.log(
    ...

    Try It: Advanced Loops

    Nested loops, break/continue, and a real-world order processing system

    Try it Yourself ยป
    JavaScript
    // Advanced Loop Patterns in PHP
    console.log("=== Nested Loops: Multiplication Table ===");
    console.log();
    
    // Build a 5x5 multiplication table
    let header = "  ร— |";
    for (let i = 1; i <= 5; i++) header += ("  " + i).slice(-3);
    console.log(header);
    console.log("  โ”€โ”€โ”ผโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€");
    
    for (let i = 1; i <= 5; i++) {
        let row = ("  " + i).slice(-2) + " |";
        for (let j = 1; j <= 5; j++) {
            row += ("  " + (i * j)).slice(-3);
        }
        console.log(row);
    }
    
    console.log();
    console.log("===
    ...

    โš ๏ธ Common Mistakes

    โš ๏ธ
    Infinite loops โ€” forgetting to increment the counter or update the condition. Always ensure the loop can terminate!
    โš ๏ธ
    Off-by-one errors โ€” using <= vs <. Decide: do you start at 0 or 1?
    โš ๏ธ
    Modifying array inside foreach โ€” can cause unexpected behaviour. Use a separate array for modifications.
    ๐Ÿ’ก
    Pro Tip: Always use foreach for arrays in PHP โ€” it's faster, cleaner, and less error-prone than for with index.

    ๐Ÿ“‹ Quick Reference โ€” PHP Loops

    StatementPurpose
    for ($i=0; $i<n; $i++)Loop n times
    foreach ($arr as $v)Iterate values
    foreach ($arr as $k=>$v)Iterate key-value pairs
    breakExit loop immediately
    continueSkip to next iteration

    ๐ŸŽ‰ Lesson Complete!

    You've mastered PHP loops! Next, learn how to write reusable functions.

    Sign up for free to track which lessons you've completed and get learning reminders.

    Previous

    Cookie & Privacy Settings

    We use cookies to improve your experience, analyze traffic, and show personalized ads. You can manage your preferences below.

    By clicking "Accept All", you consent to our use of cookies for analytics and personalized advertising. You can customize your preferences or reject non-essential cookies.

    Privacy Policy โ€ข Terms of Service