By Shubham G on Dec 06, 2023

This PHP script uses nested `for` loops to print an inverted right-angle 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 prints the asterisks. It starts from 5 minus the current line number and decrements down to 1, so the number of asterisks decreases as you go down the lines, creating the shape of the inverted right-angle.

- After the asterisks are printed for a line, a line break (`<br>`) is printed to move to the next line.


The size of the inverted right-angle (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. It's a great exercise for those learning PHP and wanting to improve their understanding of loops.

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

Output : 


  • Related Posts
Loading...