In common practice, FOR loop is mostly used when we need to loop a code for specific number of times. Whereas WHILE loops are best used when we are using conditions and number of loop is not of primary priority.
THE WHILE LOOP
As said above, the while statement executes a block of code until the condition isn’t fulfilled.
Here is the syntax of while loop.
while (condition){
code needed to loop;
}
The above code between { } will be executed until the condition turns to false.
Here is a proper PHP example.
<?php
$i = 0;
while ($i <= 10){
echo "The number is ". $i . "<br/>";
$i++;
}
?> The above program will show output Something like.
The number is 0
The number is 1
The number is 2
…and so on.
it will show 11 outputs, from 0 to 10.
THE DO WHILE LOOP
This loop is almost the same as the WHILE loop except that it executes the code atleast once before the condition
is checked. The reason for this is that the condition statement is written at the end.
Here is a DO WHILE loop syntax.
do{
code needed to loop;
}while (condition);
if we want to write the same above program with DO WHILE
<?php
$i = 0;
do{
echo "The number is " . $i . "<br/>";
}while ($i <= 10);
?> The above code will give the same output.
THE FOR LOOP
As mentioned above, the FOR loop is mostly used when we know how many times we need to loop the code. FOR loop is also known as definite loop. FOR loop is prefered by most programmers because its more convenient to use. The
syntax of FOR loop is given as follows.
for (initialization; condition; increment){
code needed to loop;
}
FOR loop takes three arguments separated by (
semicolons. Although the required argument is the condition, the other two are optional.
The initialization argument is a variable which we use for incrementation/decrementation.
The condition is the expression which is evaluated everytime the code is executed and the code loops if the condition is true.
The increment or decrement updates the counter variable.
Given below is a PHP example.
<?php
for ($i = 0; $i <= 10; $i++){
echo "The number is " . $i . "<br/>";
}
?> Next Comes the FOREACH Loop
THE FOREACH LOOP
This is basically a variation of the FOR loop and allows to iterate over array elements. The two different
versions of the Foreach loop are written below.
foreach (array as value){
code needed to loop;
}
foreach (array as key => value){
code needed to loop;
}
given below is a PHP based example.
<?php
$num = array('1', '2', '3');
foreach ($num as $value){
echo "Num " . $value . "<br/>";
}
?> Foreach’s different type is more useful.
<?php
$num = array('1' => 'One', '2', => 'Two');
foreach ($num as $key => $value){
echo "Num " . $key . " is also called " . $value . "<br/>";
}
?> In the above example, the key for each element is placed in the $key variable and its corresponding value in $value.
Although, Foreach construct does not operate on the the array itself, but rather on a copy of it. This means that what ever you change or manipulate of the $value, it wouldn’t effect the orignal array.
Break and Continue Statements
These statements are not used that often. Although they are sometimes really useful when saving some resources.
As the statements imply, Break is used to break the loop and continue is used to jump to the next iteration without executing the code beneath it.
Two examples are written below.
<?php
for ($i = 0; $i <= 10; $i++){
if($i==5){break;}
echo "The number is " . $i . "<br/>";
}
?> The above program will break the loop when $i = 5;
it will show previous outputs.
Below is an example of continue.
<?php
for ($i = 0; $i <= 10; $i++){
if($i==5){continue;}
echo "The number is " . $i . "<br/>";
}
?> The above program will skip the loop when $i = 5; It will show the prevous and next values.
I hope this tutorial becomes handy to those who are new to PHP.
Enjoy
