Showing posts with label Triangle of Stars with for loop in PHP. Show all posts
Showing posts with label Triangle of Stars with for loop in PHP. Show all posts

Friday, 9 December 2016

Star pattern programs with for loop in php


Star pattern programs with for loop in php

Script for following pattern

*
**
***
****
*****
******
*******
********

<?php
for($i=1;$i<=8;$i++){
echo str_repeat('*',$i);
echo "<br/>";
}

OR

for($i=1;$i<=8;$i++){

   for($j=1;$j<$i;$j++){
echo '*';
   }
   echo '<br/>';
}

?>


Script for following pattern

****
****
****
****
****
****

<?php
for ($i=1;$i<=6;$i++){
for($j=1;$j<5;$j++){
echo '*';
}
echo '<br/>';
}
?>


Script for following pattern

*****
****
***
**
*

<?php
for($i=6;$i>0;$i--){
for($j=1;$j<$i;$j++){
echo '*';
}
echo '<br/>';
}
?>

Script for Triangle of Stars in PHP

        *
       **
      ***
     ****
    *****
   ******

<?php
$l = 1;
for($i=8;$i>1;$i--){

for($j=1;$j<$i;$j++){
echo '&nbsp;';
}


for($k=1;$k<$l;$k++){
echo '*';
}
$l++;
echo '<br/>';

}

OR

$l = 1;
for($i=8;$i>1;$i--){

echo str_repeat('&nbsp;',$i);
echo str_repeat('*',$l++);
echo '<br/>';
}

?>