Alphabet Triangle Method
Alphabet Triangle Method
There are three methods to print the alphabets in a triangle or in a pyramid form.
- range() with for loop
- chr() with for loop
- range() with foreach loop
Logic:
- Two for loops are used.
- First for loop set conditions to print 1 to 5 rows.
- Second for loop set conditions in decreasing order.
Using range() function
This range function stores values in an array from A to Z. here, we use two for loops.
Example:
<?php
$alpha = range('A', 'Z');
for($i=0; $i<5; $i++){
for($j=5; $j>$i; $j--){
echo $alpha[$i];
}
echo "<br>";
}
?>

?php
for( $i=65; $i<=69; $i++){
for($j=5; $j>=$i-64; $j--){
echo chr($i);
}
echo "<br>";
}
?>

<?php
$k=1;
foreach(range('A','Z') as $char){
for($i=5; $i>=$k; $i--){
echo $char;
}
echo "<br>";
$k=$k+1;
