Create Left Angle Triangle Pattern in PHP Using For Loop
This PHP script uses nested `for` loops to print an inverted, left-angle triangle pattern made up of asterisk (`*`) characters.
- The outer `for` loop runs 5 times, starting from 5 and decrementing down to 1. Each iteration of this loop represents a line in the pattern.
- The first inner `for` loop prints spaces. It runs as many times as the current line number, so the number of spaces decreases as you go down the lines.
- The second inner `for` loop prints the asterisks. It starts from 5 and decrements down to the current line number, so the number of asterisks increases as you go down the lines, creating the shape of the inverted left-angle triangle.
- After the asterisks and spaces are printed for a line, a line break (`<br/>`) is printed to move to the next line.
The size of the left-angle triangle (i.e., the number of lines) can be adjusted by changing the initial and final values in the outer `for` loop. This script is a simple demonstration of how nested loops can be used to create patterns in PHP. It's a great exercise for those learning PHP and wanting to improve their understanding of loops.
<?php
for ($a = 5; $a >= 1; $a--)
{
for ($b = 1; $b <= $a; $b++)
{
echo "  ";
}
for ($c = 5; $c >= $a; $c--)
{
echo " * ";
}
echo "<br/>";
}
?>
Output :