Create patterns using PHP Loops

0
38
views
PHP for loop patterns

Learn how to create patterns by using PHP Loops.

In this tutorial we are creating some patterns by using PHP loops. Basically we are using php for loop to create this. So let’s create those patterns –

First Pattern

pattern

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

code

<?php
for($i=5; $i>0; $i--){
    for($j=$i; $j>0; $j--){
        echo "*  ";
    }
echo "<br>";
}
?>

Second Pattern

pattern

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

code

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

Third Pattern

pattern

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

code

<?php
for($i=1; $i<=7; $i++){
    if($i < 7){
        for($j=$i; $j<=$i; $j++){
            for($k=1; $k<=$j; $k++){
                echo "* ";
            }
            echo "<br>";
        }
    }
    else{
        for($j=5; $j>0; $j--){
            for($k=$j; $k>0; $k--){
                echo "* ";
            }
            echo "<br>";
        }
    }
}
?>

Fourth Pattern

pattern

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

code

<?php
for($i=1; $i<=5; $i++){
    for($k=5;$k>=$i;$k--){
        echo " ";
    }
    for($j=1; $j<=$i; $j++){
        echo "* ";
    }
    echo "<br>"; 
}
for($i=5; $i>0; $i--){
    for($k=5;$k>=$i;$k--){
        echo " ";
    }
    for($j=$i; $j>0; $j--){
        echo "*  ";
    }
    echo "<br>";
}
?>

LEAVE A REPLY

Please enter your comment!
Please enter your name here