How to Create Triangle in PHP

in this post i will share script which will generate triangle using php, this is very common type question in interview. The programming languages can be vary but question will same how to generate triangle.
Checkout other PHP Interview Questions,

Below is sample code in php to generate Right Angle:

Select Code
1
2
3
4
5
6
7
/* right tringle */  
for ($i=1; $i<=10; $i++) {  
 for($j=1; $j<=$i; $j++) {  
    echo " * ";  
  } 
echo "<br>\n";
}

Below is sample code in php to generate Triangle:

Select Code
1
2
3
4
5
6
7
8
9
10
for ($i=9; $i>=1; $i--) {  
  for($j=1; $j<=$i; $j++) {  
     echo '&nbsp;';  
  }
  $j--;
for ($k=1; $k<=(10-$j); $k++) {  
    echo " # ";   
}  
echo "<br>\n";
}

You can download source code and Demo from below link.