php
  1. php-star-triangle

PHP Star Triangle

Syntax

for ($row=1; $row<=n; $row++)
    {
        for ($col=1; $col<=$row; $col++)
          {
            echo "*";
          }
        echo "<br>";
    }

Example

$n = 5;
for ($row=1; $row<=$n; $row++)
    {
        for ($col=1; $col<=$row; $col++)
          {
            echo "*";
          }
        echo "<br>";
    }

Output

*
**
***
****
*****

Explanation

This program uses nested loops to print a star triangle pattern. The outer loop is used to traverse through the rows, while the inner loop prints a certain number of stars in each row, as determined by the current row. The variable $n determines the total number of rows in the pattern.

Use

This program can be used to create a visual pattern for decoration or to practice loop control. It can also be used as a template for creating other types of nested patterns.

Important Points

  • The inner loop in this program is dependent upon the current row number.
  • The "
    " HTML tag is used to create line breaks between each row.

Summary

The PHP star triangle program uses nested loops to print a pattern of stars in a triangle shape. The outer loop determines the number of rows, while the inner loop prints a certain number of stars in each row. This program is useful for creating visual patterns and practicing loop control.

Published on: