For loop in PHP : Introduction with Example

The PHP for Loop:



The for loop is used when you know how many times you want to execute a statement or a block of statements.

Today we are going to learn for loop in php.

Syntax:

for (init counter; test counter; increment counter) {
    code to be executed;
}

Parameters:

init counter: Initialize the loop counter value
test counter: Evaluated for each loop iteration. If it evaluates to TRUE, the loop continues. If it evaluates to FALSE, the loop ends.
increment counter: Increases the loop counter value

The example below displays the numbers from 0 to 10:

Example
<?php 
for ($x = 0; $x <= 10; $x++) {
    echo "The number is: $x <br>";

?>

OUTPUT:
The number is: 0
The number is: 1
The number is: 2
The number is: 3
The number is: 4
The number is: 5
The number is: 6
The number is: 7
The number is: 8
The number is: 9
The number is: 10




No comments:

Post a Comment

We are here to listen you, Comment your valueable opinion...!!!