Hello friends, this is my fourth tutorial of PHP and Today I am sharing the concept of using loops in PHP. Loops provides the feature of iterating through the same block desired number of times. There are different types of loops that can be used in PHP. So without wasting any time lets start.
Using while loop in PHP :
 <?php 
$c=1;
while($c<=10) {
 echo $c . ", ";
 $c+=1;
}
?>
Using for loop in PHP :
 <?php 
for($c=1;$c<=10;$c++) {
 echo $c . ", ";
}
?>
Using for-each loop in PHP :
 <?php 
$arr1=array(10,20,30,40,50);
foreach($arr1 as $var1) {
 echo $var1 . " ,";
}
//Another approach
foreach($arr1 as $pos=>$val) {
 echo $pos . " Contains " . $val . "
"; } ?>
Using for loop with continue and break statement in PHP :
 <?php 
for($c=1;$c<=10;$c++) {
 if($c==5)continue;
 if($c==8)break;
 echo $c . ", ";
}
?>

0 comments:

Post a Comment

Note: only a member of this blog may post a comment.