Pyramid Pattern

<?php
function pyramidNormal($n) {
    for ($i = 1; $i <= $n; $i++) {
        // Print spaces
        for ($j = $i; $j < $n; $j++) {
            echo " ";
        }
        // Print stars with spaces
        for ($k = 1; $k <= $i; $k++) {
            echo "* ";
        }
        echo "\n";
    }
}

// Example
pyramidNormal(4);
?>
$n = 5;
$a = $n-1;
$m = $n+1;

for($i=1;$i<=$n;$i++){
  $k = 1;
  for($j=1;$j<=$n*2-1;$j++){
    if($j>=$m-$i && $j<=$a+$i && $k==1){
      echo "*";
      $k=0;
    }else{
     echo " "; 
     $k=1;
    }
  }
  echo "\n";
}

Share: 

No comments yet! You be the first to comment.

Leave a Reply

Your email address will not be published. Required fields are marked *