PHP Star Pattern Programs

Beginner
6 min read
Updated: Nov 2026
3 examples
echo + PHP_EOL

Definition and Usage

A PHP star pattern is a script that prints * in rows. The outer for loop chooses the row. The inner for loop echos each star, then echo PHP_EOL ends that row.

echo "*" does not add a newline, so stars sit next to each other. The + operator does not add a star: '*' + 1 is the number 1, because PHP casts '*' to 0. Two stars are "*" . "*", or str_repeat("*", 2).

str_repeat prints a whole run.

echo str_repeat("*", $row) . PHP_EOL writes one solid row. Use the inner loop when that row mixes spaces and stars.

PHP Syntax

The inner loop runs once per star. echo "*" stays on the row, and PHP_EOL comes after that loop.

PHP
<?php
for ($row = 1; $row <= $rows; $row++) {
    for ($col = 1; $col <= $row; $col++)
        echo "*";         // same row
    echo PHP_EOL;         // end the row
}

How PHP Prints a Star

Map each part of the console shape to a PHP value or call.

PiecePHPWhat it does
Starecho "*"Writes one star and stays on the row
Run of starsstr_repeat("*", $n)A string of $n stars
Indentstr_repeat(" ", $n)$n spaces
Join"*" . "*"The string "**"
Numeric +"*" + 1Addition: "*" becomes 0
New rowecho PHP_EOLEnds the row
Edge onlyecho ($col == 1 || $col == $width) ? "*" : " "A hollow row

PHP Examples

Three scripts. Each one changes only the PHP that chooses how many stars to print, and where the spaces go.

Example 1 — One star per inner-loop pass

Row 1 echoes one star. Each later row echoes one more, because the inner loop stops at $row.

PHP
<?php
$rows = 5;

for ($row = 1; $row <= $rows; $row++) {
    for ($col = 1; $col <= $row; $col++) {
        echo "*";
    }
    echo PHP_EOL;
}

How It Works

  1. The outer loop walks $row from 1 to 5.
  2. echo "*" stays on the row, so the stars form one line.
  3. echo PHP_EOL after the inner loop starts the next row.

Example 2 — Print the whole row with str_repeat

Count $row down from 5. One call writes every star on that line.

PHP
<?php
$rows = 5;

for ($row = $rows; $row >= 1; $row--) {
    echo str_repeat("*", $row) . PHP_EOL;
}

How It Works

  1. str_repeat("*", $row) returns the stars as one string.
  2. The dot joins PHP_EOL onto that string.
  3. $row-- makes each later line one star shorter.

Example 3 — Indent, then print an odd run of stars

Spaces come first. The star count is 2 * $row - 1, so the pyramid grows by two stars each row.

PHP
<?php
$rows = 5;

for ($row = 1; $row <= $rows; $row++) {
    echo str_repeat(" ", $rows - $row);
    echo str_repeat("*", 2 * $row - 1);
    echo PHP_EOL;
}

How It Works

  1. Row 1 prints 4 spaces and 1 star. Row 5 prints 0 spaces and 9 stars.
  2. Both runs are strings. echo writes them on the same row.
  3. echo PHP_EOL still closes the row after the spaces and the stars.

11 PHP Star Pattern Programs

Each card is a full PHP script: nested for loops, echo "*" or str_repeat, and PHP_EOL. Open one for the walkthrough.

Frequently Asked Questions

The + operator adds. PHP casts '*' to 0, so '*' + 1 is 1. A star is echo "*". Two stars are "*" . "*", or str_repeat("*", 2).
echo "*" writes the star and stays on the same row. echo PHP_EOL ends the row. "\n" does the same break in a CLI script. If echo PHP_EOL is inside the inner loop, each star lands on its own line.
Yes, when the row is a solid run of stars. echo str_repeat("*", $row) . PHP_EOL prints that row in one call. Use the inner loop when the row mixes spaces and stars, or when only the edges should be stars.
Print the indent first: echo str_repeat(" ", $rows - $row). Then echo str_repeat("*", 2 * $row - 1). echo PHP_EOL ends the row. Row 1 has one star; each later row adds two.

Next: Star Pattern 1

A full PHP walkthrough of the right-angled star triangle, from echo "*" through echo PHP_EOL.

Star Pattern 1 →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

28 people found this page helpful