Search⌘ K
AI Features

Equivalence of Looping Structures

Explore how PHP for loops and while loops perform equivalent iteration tasks. Understand the differences in syntax and style, and learn when to use each loop construct effectively. This lesson clarifies how to rewrite a for loop as a while loop and discusses best practices in choosing between them.

For loop

An example of for loop is given below:

PHP
<?php
$i;
for ($i = 0;$i < 10;$i++)
{
$i = $i * 2;
echo "Value of i is: $i\n";
}
echo "Final value of i is: $i\n";
?>

Converting For Loop to While Loop

The above for loop can easily be rewritten as a while loop. ...