Loading...
Loading...
00:00:00

PHP Loop 

A loop in PHP is a programming construct that allows you to execute a block of code repeatedly based on a certain condition. The basic idea behind a loop is to iterate through a set of instructions until a specific condition is met, at which point the loop terminates and the program moves on to the next statement.

In PHP, there are four types of loops:

  1. for loop: A for loop allows you to execute a block of code for a specific number of times. The syntax of a for loop is for (initialization; condition; increment/decrement) { code to be executed; }

  2. while loop: A while loop allows you to execute a block of code repeatedly as long as a certain condition is true. The syntax of a while loop is while (condition) { code to be executed; }

  3. do-while loop: A do-while loop is similar to a while loop, except that it executes the block of code at least once before checking the condition. The syntax of a do-while loop is do { code to be executed; } while (condition);

  4. foreach loop: A foreach loop is used specifically for iterating through arrays or objects. The syntax of a foreach loop is foreach (array as value) { code to be executed; }

Loops are a powerful and essential tool for many programming tasks, especially when working with large datasets or when you need to perform the same operation on a set of data multiple times. By using loops, you can write more efficient and streamlined code that is easier to read and maintain.

for loop in PHP

  1. for loop: The for loop is used when you know how many times you want to execute a block of code. It has three expressions separated by semicolons: initialization, condition, and increment/decrement. The syntax for a for loop in PHP is as follows:
for (initialization; condition; increment/decrement) {
    // code to be executed
}

Here is the Example of for loop which is printing number from 0 to 9:

for ($i = 0; $i < 10; $i++) {
    echo $i . "<br>";
}

while loop in PHP

  1. while loop: The while loop is used when you want to execute a block of code while a certain condition is true. The syntax for a while loop in PHP is as follows:
while (condition) {
    // code to be executed
}

Here is the Example of while loop which is print number from 0 to 9:

$i = 0;
while ($i < 10) {
    echo $i . "<br>";
    $i++;
}

do while loop in PHP

  1. do while loop: The do-while loop is similar to the while loop, but it executes the block of code at least once before checking the condition. The syntax for a do-while loop in PHP is as follows:
do {
    // code to be executed
} while (condition);

Here is the example of do while loop which is printing number from 0 to 9:

$i = 0;
do {
    echo $i . "<br>";
    $i++;
} while ($i < 10);

foreach loop in PHP

  1. foreach loop: The foreach loop is used to iterate over an array or an object. It assigns each value in the array or object to a variable and then executes a block of code for each value. The syntax for a foreach loop in PHP is as follows:
foreach ($array as $value) {
    // code to be executed
}

Here is the example of foreach loop which is print elements of an array:

$array = array("apple", "banana", "cherry");
foreach ($array as $value) {
    echo $value . "<br>";
}

break and continue 

  1. break and continue statements: The break statement is used to exit a loop early if a certain condition is met. The continue statement is used to skip the current iteration of a loop and move on to the next iteration. Both statements can be used with any type of loop.

     

    The continue statement is used within loops to skip the current iteration of the loop and move on to the next iteration.

    When the continue statement is encountered, the current iteration of the loop is stopped and the loop proceeds to the next iteration. Any code within the loop that comes after the continue statement is skipped for the current iteration.

Here is an example that demonstrates the usage of break and continue with loops in PHP:

for ($i = 0; $i < 10; $i++) {
    if ($i == 5) {
        break;
    }
    echo $i . "<br>";
}

for ($i = 0; $i < 10; $i++) {
    if ($i == 5) {
        continue;
    }
    echo $i . "<br>";
}

Break in PHP

In PHP, the break statement is used within loops to immediately exit the loop and continue with the rest of the program.

When the break statement is encountered, the loop stops iterating and the program continues executing the code that comes after the loop. Any code within the loop that comes after the break statement is skipped.

Here's an example of how to use break in a while loop:

$i = 1;
while ($i <= 10) {
  if ($i == 5) {
    break;
  }
  echo $i . "<br>";
  $i++;
}

In this example, the while loop will iterate through the numbers 1 to 10. However, when $i is equal to 5, the break statement is executed, immediately exiting the loop. The output of this code will be 1 2 3 4.

As you can see, the loop stopped iterating when $i became 5, and the rest of the code within the loop was skipped.

The break statement can be used with any type of loop in PHP, including for, while, do-while, and foreach loops. It can be useful in situations where you want to exit a loop early based on a certain condition, or when you want to stop iterating through a large dataset once you have found what you are looking for.

Continue in PHP

In PHP, the continue statement is used within loops to skip the current iteration of the loop and move on to the next iteration. continue statement can be use with any type of loops in php.

When the continue statement is encountered, the current iteration of the loop is stopped and the loop proceeds to the next iteration. Any code within the loop that comes after the continue statement is skipped for the current iteration.

Here's an example of how to use continue in a for loop:

for ($i = 1; $i <= 10; $i++) {
  if ($i == 5) {
    continue;
  }
  echo $i . "<br>";
}

In this example, the for loop will iterate through the numbers 1 to 10. However, when $i is equal to 5, the continue statement is executed, skipping the rest of the code in the loop for that iteration. The output of this code will be  1 2 3 4 6 7 8 9 10.