PHP For Loop

Learn via video courses
Topics Covered

Overview

In PHP, a for loop is a control structure used to repeatedly execute a block of code based on a specified condition. The basic syntax of a for loop consists of three parts: initialization, condition, and increment/decrement. The initialization part sets the starting value of the loop variable, the condition part specifies the condition that must be true for the loop to continue executing, and the increment/decrement part modifies the value of the loop variable after each iteration. In addition to the basic syntax, the for loop can be extended with additional features such as a break statement to exit the loop early or a continue statement to skip to the next iteration.

Introduction

In PHP, a loop is a programming construct that allows you to repeat a block of code multiple times. One of the most commonly used loops in PHP is the for loop. A for loop is used when you know exactly how many times you want to repeat a block of code.

The syntax of a for loop in PHP consists of three parts: the initialization, the condition, and the increment. In the initialization part, you declare and initialize a variable that will be used as a counter. The condition part checks whether the counter has reached a certain value.

Now let us read about the flowchart of for loop in PHP:

PHP for Loop Flowchart

. A PHP for loop flowchart depicts the control flow of a for loop in PHP programming.

The flowchart for a for loop in PHP consists of several basic elements:

  • Start/End: The first and last elements of the flowchart, which indicate the beginning and end of the loop.
  • Initialization: This is where the loop counter is initialized to its starting value.
  • Condition: The condition part of the loop checks whether the counter has reached a certain value. If the condition is true, the loop continues to execute; if it is false, the loop terminates.
  • Block of code: This is the block of code that is executed each time the loop iterates. It can consist of any valid PHP code, including conditionals, functions, and loops.
  • Increment/Decrement: This is the step that updates the counter after each iteration of the loop. It can be an increment or decrement operation, depending on the direction of the loop.

Now let us see the syntax of for loop in PHP:

Syntax:

The "for" loop in PHP is a type of loop that allows you to execute a block of code repeatedly for a specified number of times. It has a specific syntax that consists of three parts: the initialization, the condition, and the increment.

The Syntax of a "for" loop in PHP is as follows:

Parameters

In PHP, a for loop accepts three parameters inside its parentheses. These parameters are used to control the flow of the loop and determine when it starts when it ends, and how it increments or decrements. The parameters are separated by semicolons, and each one serves a different purpose. Let us read about the parameters of a for loop in PHP with examples:

  1. Initialization: The initialization parameter initializes a loop variable before the loop starts executing. It typically declares and initializes a counter variable. This parameter is executed only once at the beginning of the loop. For example:
  1. Condition: The condition parameter specifies a condition that must be true for the loop to continue executing. If the condition is false, the loop terminates. This parameter is evaluated before each iteration of the loop. For example:
  1. Increment/Decrement: The increment parameter updates the loop variable after each iteration. It typically increments or decrements the counter variable. This parameter is executed at the end of each iteration of the loop. For example:

Now let us see a few examples of for loops in PHP:

Example

1. Simple Example Using for Loop

Output

2. All Three Parameters are Optional, but Semicolon (;) is Must to Pass in For Loop

Let us see an example of a for loop with all three parameters omitted, but semicolons included:

3. Printing Numbers From 1 to 9 in Four Different Ways Using for Loop

  • Printing numbers using a basic for loop:

Output

Output

In this example, we are using the range() function to create an array of numbers from 1 to 9, and then using a for each loop to iterate over the array and print each number.

4. Printing numbers in reverse order using a for loop

Output:

In this example, We are Using a For Loop to Print Numbers from 9 to 1 in Reverse Order. The Loop Starts at 9, Continues Until $i is Greater Than or Equal to 1, and Decrements $i by 1 After Each Iteration.

5. Printing Only Even Numbers Using a For Loop

Output

In this example, we are using a for loop to print only the even numbers from 0 to 9. The loop starts at 1, continues until $i is less than or equal to 9, and increments $i by 2 after each iteration to skip odd numbers.

PHP Nested Loop

A nested loop in PHP is a loop inside another loop. The inner loop is executed multiple times for each iteration of the outer loop. Nested loops are useful when you need to perform a certain task on each combination of two or more sets of data.

Let us see an example of a nested loop that prints a multiplication table:

In the example above, we have two for loops. The outer loop iterates from 1 to 10 and the inner loop also iterates from 1 to 10. For each iteration of the outer loop, the inner loop is executed 10 times. Inside the inner loop, we multiply the current values of $i and $j and print the result with a tab character using echo. At the end of each row, we print a newline character to move to the next line.

Conclusion

  • The for loop is a control structure in PHP that allows you to repeat a set of statements a specified number of times.
  • The syntax of a for loop in PHP consists of an initialization statement, a condition, and an increment statement, all enclosed within parentheses and separated by semicolons.
  • The initialization statement is executed only once at the beginning of the loop, while the condition is checked at the beginning of each iteration.
  • If the condition is true, the loop executes the statements within the curly braces, and then the increment statement is executed.

MCQs

  1. What is the purpose of a for loop in PHP?

    i) To repeat a set of statements a specified number of times.
    ii) To execute a set of statements until a condition becomes true.
    iii) To switch between different sets of statements based on a condition.
    iv) To create a new variable with a specific value.

Answer: i)

  1. Which of the following statements is executed only once in a for loop in PHP?

    i) Initialization statement
    ii) Condition
    iii) Increment statement
    iv) All of the above are executed in each iteration

Answer: i)

  1. What is the purpose of the break statement in a for loop in PHP?

    i) To exit the loop immediately
    ii) To skip over the current iteration and continue with the next one
    iii) To change the value of the loop counter
    iv) To repeat the loop from the beginning

Answer: i)