There are times in any Script that you need to repeat certain part of the code for a few times. there are two main ways to address this in PHP is to use a loop or a recursive function in this Part of our PHP training course we will talk about loops:
There a few ways to make a loop but for me you can categorise it in two main different ways :
- When you know how many times you need to repeat a loop
for this you can use one of the two methods below:
- counter method
use a counter to count how many you have done this part of the code and check it to see if it is the amount that you were looking for or you need to repeat more here is an example:
$count=0;
$finally=10;
$result=0;
while($count!=$finally){// as long as they are not the same it will be executed
$result=$result+$count;
$count++;
}
echo $result;// it will return 45 because 45=0+1+2+3+4+5+6+7+8+9
it won’t run the last because in that one $count and $finally are the same but you can use another while method for this as well here is the example:
$count=0;
$finally=10;
$result=0;
do{
$result=$result+$count;
$count++;
}while($count!=$finally);
echo $result;// it will return 55 because 55 is 1+2+3+4+5+6+7+8+9+10 it will execute the block of the code inside the do while as least once and it will continue as long as the condition of the while is true;
- for loop:
for loops are the simples way to do this and here is an example:
$result=0;
for ($count=0; $count<10; $count++){
$result=$result+$count;
}
echo $result;// 0+1+2+3+4+5+6+7+8+9 thats is 45
- When you don’t know how many times you need to repeat a loop
There are a lots of times that you need to repeat a script but you don’t know how man times like when you getting results from a database search or if you want to work with arrays(just for this example but you can use for loop or a counter method for arrays using count() function on an array to tell you how many items are in it but this is a faster and easier way). You can do this using the ollowing methods:
this method is specifically for working with arrays(most database management systems will return your search result in an array and that is where is loop statement becomes invalueable) and here is an example:
$items=array(2,3,4,6);
$result=0;
foreach($items as $item){
$result=$result+$item;
}
echo $result;// the result is 15
- while
as described before while will repeat the block of code as long as it’s condition is true, you can use this to creat an infinit loop although I don’t recommend it. here is an example
$result=0;
$items=array(false, 3, “some text”);
$count=0; //this is not as the count before as this one is not part of the condition
if($items[$count]==”some text”){
echo “This is the text”;
break;// it will finish the loop
}else{
echo “this was not the text”;
$count++; // the reason that I don’t recommend you do an infinit loop is that if you forget this line of the code the loop will never come to an end!
}
}
- do…while
the same as before do…while will execute the block once and then it will check the condition if it is true will repeat and if it is false it will exite the loop:
$result=0;
$counter=0;
$mystring=”this is a text”;
$length=strlen($mystring);// it will set $length as 14
do{
echo $mystring[$counter].”-“;
$counter++;
}while($length!=$counter);
// the result of this code is: t-h-i-s- -i-s- -a- -t-e-x-t-