Creating Patterns (Demo) using for loop

Pattern #1 :

Creating pattern of stars in ten rows.
*
**
***
****
*****
******
*******
********
*********
**********
Source code is here
for($i=1; $i<=10; $i++){
for($j=1;$j<=$i;$j++){
echo "*";
}
echo "<br>";
}

Pattern #2

*---------
**--------
***-------
****------
*****-----
******----
*******---
********--
*********-
**********
Source code is here
for($i=1; $i<=10; $i++){
for($j=1;$j<=$i;$j++){
echo "*";
}
for($k=$j;$k<=10;$k++){
echo "-";
}
echo "<br>";
}

Pattern #3

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

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

Pattern #4

          *
         ***
        *****
       *******
      *********
     ***********
    *************
   ***************
  *****************
 *******************
Use &nbsp; inside first echo to add empty space.
for($i=1;$i<=10;$i++){
  for($j=$i;$j<=10;$j++){
    echo '&nbsp;';
  }
   for($k=1;$k<=(2*$i-1);$k++){
    echo '*';
  }
  echo ('<br>');
}

Pattern #5

 *******************
  *****************
   ***************
    *************
     ***********
      *********
       *******
        *****
         ***
          *
           
for($i=10;$i>=0;$i--){
  for($j=$i;$j<=10;$j++){
    echo '&nbsp;';
  }
   for($k=1;$k<=(2*$i-1);$k++){
    echo '*';
  }
  echo ('<br>');
} 

Pattern #6

************************
***********  ***********
**********    **********
*********      *********
********        ********
*******          *******
******            ******
*****              *****
****                ****
***                  ***
**                    **
*                      *
                        
for($i=0;$i<=12;$i++){
  for($j=$i;$j<=11;$j++){
    echo '*';
  }
   for($k=1;$k<=(2*$i);$k++){
    echo '&nbsp;';
  }
  for($j=$i;$j<=11;$j++){
    echo '*';
  }
  echo ('<br>');
}

Tutorial on For loop
Subscribe to our YouTube Channel here


Subscribe

* indicates required
Subscribe to plus2net

    plus2net.com







    Sagar

    10-04-2015

    Nice Example To Useful Developing Logic in php.......Thanks

    Post your comments , suggestion , error , requirements etc here





    PHP video Tutorials
    We use cookies to improve your browsing experience. . Learn more
    HTML MySQL PHP JavaScript ASP Photoshop Articles FORUM . Contact us
    ©2000-2024 plus2net.com All rights reserved worldwide Privacy Policy Disclaimer