WEB DEVELOPER SITE
HTMLCSSJAVASCRIPTSQLPHPBOOTSTRAPJQUERYANGULARXML
 

PHP 5 for Loops



The PHP for Loop

The for loop is utilized when you know ahead of time how frequently the content ought to run.

Syntax

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

Parameters:

  • init counter: Initialize the loop counter value
  • test counter: Evaluated for each loop iteration. In the event that it assesses to TRUE, the loop proceeds. On the off chance that it assesses to FALSE, the loop ends.
  • increment counter: Increases the loop counter value
Each of the parameter articulations can be unfilled or contain numerous articulations that are isolated with commas. In the for explanation, the parameters are isolated with semicolons.

The model underneath showcases the numbers from 0 to 5:

Example

<?php
for ($x = 0; $x <= 5; $x++) {
    reverberation "The number is: $x <br>";
}
?>
/Output
 

Estimation of a : 0 

Estimation of a : 1 

Estimation of a : 2 

Estimation of a : 3 

Estimation of a : 4 


The PHP foreach Loop

The foreach loop works just on clusters, and is utilized to loop through each key/esteem pair in an array.

Syntax

foreach ($array as $value) {
    code to be executed;
}

The first structure loops over the cluster. On every iteration, the estimation of the present component is allocated to $value, and the cluster pointer is moved by one, until it achieves the last exhibit component. The second structure will furthermore dole out the present component's vital to the $key variable on each iteration.

The following precedent exhibits a loop that will yield the estimations of the given cluster ($colors):

Example

<?php
$colors = array("white", "red", "pink", "yellow");

foreach ($colors as $value) {
    reverberation "$value <br>";
}
?>