PHP Loop Example



While Loop Example.
<?php
	$count = 1;
	while($count <= 15)
	{
		echo("Count is $count");
		echo </br>;
		$count = $count + 1;
	}	
?>
Do While Loop Example.
<?php
	$count = 1;
	do 
	{
		echo("Count is $count");
		echo </br>;
    }
	while($count <  15);
?>
For Loop example
<?php
	for($count = 0; $count <= 15; $count++) {
		echo("Count is $count");
		echo </br>;
	}
?>
Break In Loop Example
<?php
	for($count = 0; $count <= 15; $count++) {
		if($count % 2 != 0) {
			break;
		}
		echo("Count is $count");
		echo </br>;
	}
?>
Continue In Loop Example
<?php
	for($count = 0; $count <= 15; $count++) {
		if($count % 2 != 0) {
			continue;
		}
		echo("Count is $count");
		echo </br>;
	}
?>
Pyramid Program using while loop
<?php
$i=1;
while($i<=5)
{
    $j=1;
    while($j<=$i)
    {
      echo"*  ";
      $j++;     
    }
    echo"
"; $i++; } ?>
Print array key and value using foreach
<?php
$basket = array('red'=>'Rose', 'yellow' =>'Sunflower', 'pink' => 'Lotus', 'white' => 'Lily');
foreach($basket as $key=>$value){
    echo 'Color of '.$value.' is '.$key.'<br/>';
}
?>
Print multidimensional array key and value using foreach
<?php

$combo = array('fruit' =>
                array('red' => 'Mango',
                          'orange' => 'Orange',
                          'green' => 'Graphs',
                          'yellow' => 'banana')
                'flower' =>
                array('red' => 'Rose',
                          'yellow' => 'Sunflower',
                          'pink' => 'Lotus',
                          'white' => 'Lily')
    );
foreach($combo as $name=>$data){
    foreach($data as $key=>$value){
        echo 'Color of '.$value.' '.$name.' is '.$key.'<br/>';
    }
}
?>


Read more articles


General Knowledge



Learn Popular Language