Loops in PHP


while loop

PHP while Loop can be used for a set of codes for loops

Syntax - 

while(condition){  
//code to be executed  
}  

Example - 

<?php
$i = 0;
while($i < 5)
{
    echo $i."<br>";
    $i++;
}
?>

do-while loop

loop can be used on the loop on the set of PHP code such as loop. The PHP Do While  Loop is guaranteed to run at least once.

Syntax - 

do{  
//code to be executed  
}while(condition);  


Example- 

<?php
$i = 0;
do
{
    echo $i."<br>";
    $i++;
} while($i < 0);
?>

 for loop

The PHP loop can be used to set the code for a specific time.

Syntax - 

for(initialization; condition; increment/decrement){  
//code to be executed  
}  

Example- 

<?php  
for($n=1;$n<=10;$n++){  
echo "$n<br/>";  
}  
?>  

foreach loop

The foreach loop in PHP is itating through an array. Actually, it only works on arrays, and PHP will issue an error if you try it on an unitialized variable, or a variable which does not contain an array. The foreach loop comes in two different flavors - here is an example of the first one. Oh, and since arrays have not been introduced yet, think of them as a list of items for now. In a later chapter, we will see them

Syntax - 

foreach$array as $var ){  
 //code to be executed  
}  

Example- 

<?php  
$season=array("summer","winter","spring","autumn");  
foreach$season as $arr ){  
  echo "Season is: $arr<br />";  
}  
?>  
Share on Google Plus

About It E Research

This is a short description in the author block about the author. You edit it by entering text in the "Biographical Info" field in the user admin panel.
    Blogger Comment
    Facebook Comment

0 comments:

Post a Comment