Create Right Angle Triangle Pattern in PHP Using For Loop

By Shubham G on Dec 03, 2023

This PHP script uses nested `for` loops to print a left-aligned pyramid pattern made up of asterisk (`*`) characters. The outer `for` loop runs 6 times, representing the number of lines in the pattern. The inner `for` loop runs as many times as the current line number (represented by `$i`), printing an asterisk each time. This results in an increasing number of asterisks per line, creating the shape of the right angle triangle. After each line of asterisks, a line break (`<br>`) is printed to move to the next line. The size of the pattern (i.e., the number of lines) can be adjusted by changing the condition in the outer `for` loop. This script is a simple demonstration of how nested loops can be used to create patterns in PHP.

<?php
for ($i = 0; $i <= 5; $i++)
{
    for ($j = 1; $j <= $i; $j++)
    {
        echo " * ";
    }
    echo "<br>";
}
?>

Output :

 

  • Related Posts
Loading...