Part 6 : PHP tutorial for kids and beginners


Part 6: Loops in PHP

Welcome back to our PHP programming tutorial series! 🎉 In Part 5, we explored conditional statements in PHP and learned how to make decisions in our code using if, if-else, if-elseif-else, switch statements, and the ternary operator. Today, in Part 6, we’ll dive into Loops. We’ll learn how to use loops to repeat tasks and manage repetitive operations in our PHP programs. Let’s get started!

What Are Loops?

Loops are used to repeat a block of code multiple times until a specific condition is met. They are essential for automating repetitive tasks and iterating over arrays or other data structures.

Types of Loops in PHP

PHP supports several types of loops:

  1. for Loop
  2. while Loop
  3. do-while Loop
  4. foreach Loop

Let’s explore each type in detail.

1. for Loop

The for loop executes a block of code a specified number of times.

Syntax:

<?php
    for (initialization; condition; increment/decrement) {
        // Code to be executed
    }
?>

Example:

<?php
    for ($i = 1; $i <= 5; $i++) {
        echo "Number: " . $i . "<br>";
    }
?>

In this example, the loop will output numbers from 1 to 5.

2. while Loop

The while loop executes a block of code as long as a specified condition is true.

See also  How to restart PHP-FPM

Syntax:

<?php
    while (condition) {
        // Code to be executed
    }
?>

Example:

<?php
    $count = 1;

    while ($count <= 5) {
        echo "Count: " . $count . "<br>";
        $count++;
    }
?>

In this example, the loop will output counts from 1 to 5.

3. do-while Loop

The do-while loop is similar to the while loop, but it executes the block of code at least once, even if the condition is false.

Syntax:

<?php
    do {
        // Code to be executed
    } while (condition);
?>

Example:

<?php
    $number = 1;

    do {
        echo "Number: " . $number . "<br>";
        $number++;
    } while ($number <= 5);
?>

In this example, the loop will output numbers from 1 to 5.

4. foreach Loop

The foreach loop is used to iterate over arrays and objects.

Syntax:

<?php
    foreach ($array as $value) {
        // Code to be executed
    }
?>

Example:

<?php
    $fruits = ["Apple", "Banana", "Cherry"];

    foreach ($fruits as $fruit) {
        echo $fruit . "<br>";
    }
?>

In this example, the loop will output each element in the $fruits array.

Loop Control Statements

PHP provides control statements to change the execution flow of loops:

  • break: Terminates the loop.
  • continue: Skips the rest of the current iteration and continues to the next iteration.

Example of break:

<?php
    for ($i = 1; $i <= 10; $i++) {
        if ($i == 6) {
            break;
        }
        echo "Number: " . $i . "<br>";
    }
?>

In this example, the loop will output numbers from 1 to 5 and then terminate when $i equals 6.

Example of continue:

<?php
    for ($i = 1; $i <= 5; $i++) {
        if ($i == 3) {
            continue;
        }
        echo "Number: " . $i . "<br>";
    }
?>

In this example, the loop will skip printing “Number: 3” and continue with numbers 1, 2, 4, and 5.

See also  Getting Started with AWS S3: A Step-by-Step Guide

Basic Example

Here’s a PHP script that demonstrates the use of different loops:

<!DOCTYPE html>
<html>
<head>
    <title>Loops in PHP</title>
</head>
<body>
    <?php
        // for loop
        echo "<h3>For Loop</h3>";
        for ($i = 1; $i <= 5; $i++) {
            echo "Number: " . $i . "<br>";
        }

        // while loop
        echo "<h3>While Loop</h3>";
        $count = 1;
        while ($count <= 5) {
            echo "Count: " . $count . "<br>";
            $count++;
        }

        // do-while loop
        echo "<h3>Do-While Loop</h3>";
        $number = 1;
        do {
            echo "Number: " . $number . "<br>";
            $number++;
        } while ($number <= 5);

        // foreach loop
        echo "<h3>Foreach Loop</h3>";
        $fruits = ["Apple", "Banana", "Cherry"];
        foreach ($fruits as $fruit) {
            echo $fruit . "<br>";
        }
    ?>
</body>
</html>

Summary

In Part 6, we explored loops in PHP. We learned how to use for, while, do-while, and foreach loops to repeat tasks and iterate over arrays or other data structures. We also covered loop control statements break and continue to alter the flow of execution within loops.

What’s Next?

In Part 7, we’ll explore Functions in PHP. We’ll learn how to define functions, pass arguments, return values, and use built-in functions in PHP.

Homework

  1. Experiment: Create a PHP file and practice using different types of loops. Try nesting loops and using loop control statements.
  2. Practice: Write a PHP script that uses a loop to display a multiplication table for a given number.

Feel free to leave comments if you have any questions or run into any issues. Happy coding! 🚀


Next Part Teaser

Stay tuned for Part 7: Functions in PHP, where we will explore how to define and use functions to organize and reuse code in PHP!

Additional Resources

If you want to explore more about loops in PHP, check out these resources:

See also  Best Practices in Laravel Development

Part 7 Teaser

Coming up next in Part 7: Functions in PHP, where we will explore how to define and use functions to organize and reuse code in PHP!

Comments

No comments yet. Why don’t you start the discussion?

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.