In this article Looping Statements in PHP we give the information about Looping statements are used to execute a code block repeatedly until a certain condition is met.

Looping Statements in PHP:

Looping statements are used to execute a code block repeatedly until a certain condition is met. PHP mainly consists of the following looping statements:

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

Also break and continue statements are used to control the loop.

  1. for Loop in PHP

for loop is used when we know in advance how many times the loop should run. It executes a code block a certain number of times.

Syntax of for Loop

for (initialization; condition; increment/decrement) {

    //This code will run as long as the condition is true.

}

Description of parts of syntax

  1. Initialization:

The variable is given an initial value. It runs only once.

  1. Condition:

The loop continues as long as this condition remains true.

  1. Increment/Decrement:

The variable is incremented or decremented after every iteration.

Example 1: Print numbers from 1 to 5

<?php

for ($i = 1; $i<= 5; $i++) {

    echo “Number: $i<br>”;

}

?>

Output:

Number: 1

Number: 2

Number: 3

Number: 4

Number: 5

Example 2: Print reversed numbers from 10 to 1

<?php

for ($i = 10; $i>= 1; $i–) {

    echo “Number: $i<br>”;

}

?>

Output:

Number: 10

Number: 9

Number: 8

Number: 7

Number: 6

Number: 5

Number: 4

Number: 3

Number: 2

Number: 1

Example 3: Print only even numbers

<?php

for ($i = 2; $i<= 10; $i += 2) {

    echo “Even number: $i<br>”;

}

?>

Output:

Even number: 2

Even number: 4

Even number: 6

Even number: 8

Even number: 10

Example 4: breaking the loop

<?php

for ($i = 1; $i<= 10; $i++) {

    if ($i == 6) {

        break; //The loop will stop at 6.

    }

    echo “Number: $i<br>”;

}

?>

Output:

Number:  1

Number:  2

Number: 3

Number: 4

Number: 5

Example 5: Skipping a particular iteration (continue)

<?php

for ($i = 1; $i<= 5; $i++) {

    if ($i == 3) {

        continue; //Except 3 the rest of the numbers will be printed.

    }

    echo “Number: $i<br>”;

}

?>

Output:

Number: 1

Number: 2

Number: 4

Number: 5

Tips for using for Loop

  1. The condition is correct:

Make sure the condition is true, otherwise the loop may become infinite.

  1. Keep in mind Increment/Decrement:

Increase or decrease the variable correctly.

  1. Use break and continue:

Use these to stop a loop or skip an iteration when needed.

Conclusion

  • The for loop in PHP is an effective way to run a code block repeatedly.
  • It is ideal for a fixed number of iterations.
  • It is considered more controlled than other loops like while and do-while.

Some More: 

POP- Introduction to Programming Using ‘C’

DS – Data structure Using C

OOP – Object Oriented Programming 

Java Programming

DBMS – Database Management System

RDBMS – Relational Database Management System

Join Now: Data Warehousing and Data Mining 

Leave a Reply

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