PHP for Loop

The first and most basic type of loop is the for loop. This loop is the same as the Javascript version of the loop. The biggest difference is the fact that the variables need a dollar sign in front of them.

for ($i = 0; $i < 10; $i++) { 
  echo "{$i} <br/>"; 
} 

This loop will write out the numbers from zero to nine with an HTML <br/> tag after each number. We could use a for loop to output all the items in a simple array.

$students = ["Chad", "Christine", "Greg", "Hanji"]; 
$num = count($students);
for ($s = 0; $s < $num; $s++) {
  echo "{$students[$s]} <br/>";
} 

This example writes out each of the four names with an HTML <br/> tag after each one.