WEB DEVELOPER SITE
HTMLCSSJAVASCRIPTSQLPHPBOOTSTRAPJQUERYANGULARXML
 

PHP 5 for Loops



The PHP for Loop

The for circle 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 circle counter value
  • test counter: Evaluated for each circle cycle. In the event that it assesses to TRUE, the circle proceeds. On the off chance that it assesses to FALSE, the circle ends.
  • increment counter: Increases the circle 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 circle works just on clusters, and is utilized to circle through each key/esteem pair in an array.

Syntax

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

The first structure circles over the cluster. On every cycle, 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 circle 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>";
}
?>