How to print a triangular pattern in php

We can print this pattern in php. For this we need Variables and ‘for’ loops. First we have Variables in which Symbols are passing as a string to print pattern. First ‘for’ loop is used for number of row and second & third ‘for’ loop is used for numbers in a row. ‘if’ condition is used to print empty space and ‘elseif’ condition used to print symbol.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
<?php
$p="|";
$q="\\";
$r=" _ ";
for($a=1; $a<=6; $a++){
  for($b=$a; $b<=$a; $b++){
    echo $p;
      for($c=1; $c<=$a-1; $c++){
    if($a <= 5){
      echo " &nbsp ";
    }
    elseif($a == 6 && $c<=$a/2){
      echo $r;
    }
  }
  echo $q;
  }
  echo "<br>";
}
?>
triangular-in-php
6 Comments

Leave a Reply