PHP programs for printing pyramid patterns

This article is aimed at giving a PHP implementation for pattern printing.
Simple Pyramid Pattern
PHP
<?php// Php code to demonstrate// star patternÂ
// Function to demonstrate// printing patternfunction pypart($n){   // Outer loop to handle number // of rows in this case for ($i = 0; $i < $n; $i++) {     // inner loop to handle  // number of columns  // values changing acc.   // to outer loop  for($j = 0; $j <= $i; $j++ )  {       // Printing stars   echo "* ";  }Â
  // ending line after  // each row  echo "\n"; }}Â
 // Driver Code $n = 5; pypart($n);?> |
Output :
* * * * * * * * * * * * * * *
Time Complexity: The time complexity of this algorithm is O(N^2) where N is the number of rows.
Space Complexity: The space complexity of this algorithm is O(1) because only a fixed amount of memory is used.
After 180 degree rotation
PHP
<?php// PHP code to demonstrate// star patternÂ
// Function to demonstrate// printing patternfunction pypart2($n){Â for ($i = 1; $i <= $n; $i++) {Â Â for ($j = 1; $j <= $n; $j++) {Â Â Â if($j<=($n-$i)){Â Â Â Â echo " "." ";Â Â Â Â Â Â Â Â }else {Â Â Â Â echo "* ";Â Â Â }Â Â Â Â Â Â }Â Â echo PHP_EOL;Â } }Â
 // Driver Code $n = 5; pypart2($n);Â
?> |
Output :
*
* *
* * *
* * * *
* * * * *
Time Complexity: O(N^2) where N is the number of rows.
Space Complexity: O(1) because only a fixed amount of memory is used.
Printing Triangle
PHP
<?php// PHP code to demonstrate// star patternÂ
// Function to demonstrate // printing patternfunction triangle($n){   // number of spaces $k = 2 * $n - 2;Â
 // outer loop to handle // number of rows // n in this case for ($i = 0; $i < $n; $i++) {     // inner loop to handle  // number spaces  // values changing acc.   // to requirement  for ($j = 0; $j < $k; $j++)   echo " ";Â
  // decrementing k after  // each loop  $k = $k - 1;Â
  // inner loop to handle   // number of columns  // values changing acc.   // to outer loop  for ($j = 0; $j <= $i; $j++ )  {       // printing stars   echo "* ";  }Â
  // ending line after  // each row  echo "\n"; }}Â
 // Driver Code $n = 5; triangle($n);  ?> |
Output :
* * * * * * * * * * * * * * *
Time complexity: O(N^2) where N is given number of rows
Auxiliary Space: O(1)
Star Triangle Pattern 2
PHP
<?php // code// PHP code to demonstrate// star pattern 2Â
// Function to demonstrate // printing pattern 2function triangle_pattern($len){$string="*";$pyramid_str="";$mid_point=ceil($len/2);for($i=1;$i<=$mid_point;$i++){Â for($j = 1; $j <= $i; ++$j) {Â Â $pyramid_str.=$string." ";Â }Â $pyramid_str.="\r\n";}Â
for($i=$mid_point;$i>=1;$i--){Â for($j = 1; $j < $i; ++$j) {Â Â $pyramid_str.=$string." ";Â }Â $pyramid_str.="\r\n"; }Â
return $pyramid_str;}echo triangle_pattern(9);?> |
Output
* * * * * * * * * * * * * * * * * * * * * * * * *
Time Complexity: O(n^2), where n is the input parameter.Â
Space Complexity: O(n), where n is the input parameter.
Number Pattern
PHP
<?php// PHP code to demonstrate // printing pattern of numbersÂ
// Function to demonstrate // printing patternfunction numpat($n){   // initializing starting number $num = 1;Â
 // outer loop to handle // number of rows // n in this case for ($i = 0; $i < $n; $i++) {Â
  // inner loop to handle  // number of columns  // values changing acc.   // to outer loop  for ($j = 0; $j <= $i; $j++ )  {       // printing number   echo $num." ";  }      // incrementing number   // at each column   $num = $num + 1;Â
  // ending line after  // each row  echo "\n"; }}Â
 // Driver Code $n = 5; numpat($n);Â
?> |
Output:
1 2 2 3 3 3 4 4 4 4 5 5 5 5 5
Numbers without re assigning
PHP
<?php// PHP code to demonstrate // printing pattern of numbersÂ
// Function to demonstrate// printing patternfunction numpat($n){   // initialising starting // number $num = 1;Â
 // outer loop to handle  // number of rows // n in this case for ($i = 0; $i < $n; $i++) {     // inner loop to handle   // number of columns  // values changing acc.  // to outer loop  for ($j = 0; $j <= $i; $j++ )  {       // printing number   echo $num." ";Â
   // incrementing number    // at each column   $num = $num + 1;  }Â
  // ending line after  // each row  echo "\n"; }}Â
 // Driver Code $n = 5; numpat($n);Â
?> |
Output:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
Character Pattern
PHP
<?php// PHP code to demonstrate printing// pattern of alphabetsÂ
// Function to demonstrate // printing patternfunction alphapat($n){   // initializing value // corresponding to 'A' // ASCII value $num = 65;Â
 // outer loop to handle  // number of rows // n in this case for ($i = 0; $i < $n; $i++) {     // inner loop to handle  // number of columns  // values changing acc.   // to outer loop  for ($j = 0; $j <= $i; $j++ )  {       // explicitly converting   // to char   $ch = chr($num);Â
   // printing char value   echo $ch." ";  }Â
  // incrementing number  $num = $num + 1;Â
  // ending line after  // each row  echo "\n"; }}Â
 // Driver Code $n = 5; alphapat($n);  ?> |
Output:
A B B C C C D D D D E E E E E
Continuous Character pattern
PHP
<?php// PHP code to demonstrate printing// pattern of alphabetsÂ
// Function to demonstrate// printing patternfunction contalpha($n){   // initializing value // corresponding to 'A' // ASCII value $num = 65;Â
 // outer loop to handle // number of rows // n in this case for ($i = 0; $i < $n; $i++) {     // inner loop to handle  // number of columns  // values changing acc.   // to outer loop  for ($j = 0; $j <= $i; $j++ )  {       // explicitly converting   // to char   $ch = chr($num);Â
   // printing char value   echo $ch." ";Â
   // incrementing number    // at each column   $num = $num + 1;  }Â
  // ending line after each row  echo "\n"; }}Â
 // Driver Code $n = 5; contalpha($n);  ?> |
Output:
A B C D E F G H I J K L M N O
Time complexity: O(n^2) where N is given input no of rows
Auxiliary Space: O(1)
Related Article: Programs for printing pyramid patterns in C++ Programs for printing pyramid patterns in Java Programs for printing pyramid patterns in Python
PHP is a server-side scripting language designed specifically for web development. You can learn PHP from the ground up by following this PHP Tutorial and PHP Examples.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 zambiatek!



