Star pattern programs print * and spaces in rows to form triangles, pyramids, diamonds, and hollow shapes. They are the classic first step into nested for loops in PHP.
Remember
Starter rule (Program 1): on row i, print i stars
*
**
***
****
***** ← 5 rows
Learn the right-angled triangle first, then reuse the same outer-row idea for inverted shapes, leading spaces, and mirrored halves. This hub teaches that foundation, then links to all 11 tutorials.
Approach
How to Solve It
Start with nested loops for the starter triangle, then optionally shorten with str_repeat.
Method
Idea
Best for
Nested loops
Outer = rows, inner = stars via echo "*"
Learning, interviews, exams
str_repeat("*", $i)
Build a whole row in one call
Shorter demos once loops click
Spaces + stars
Add a space loop before the star loop
Right-aligned / centered shapes (Programs 3–6)
Pseudocode
Pseudocode
for i from 1 to rows:
for j from 1 to i:
print "*" (no newline)
print newline
Same idea as C# Write / WriteLine: print characters without a newline, then end the row once.
Try it
Live Preview
Change the row count and the starter triangle updates instantly — including the triangular star total.
Whole numbers from 1 to 20. Tap a chip or type a value — the preview redraws as you go.
Live result5 rows · 15 stars
*
**
***
****
*****
Trace
Worked Walkthrough — rows = 4
Trace each outer-loop value of i and count how many times the inner loop runs.
i
Inner j
Printed row
Stars
1
1..1
*
1
2
1..2
**
2
3
1..3
***
3
4
1..4
****
4
Total star prints: 1 + 2 + 3 + 4 = 10 = 4×5/2. That triangular sum is why time is O(n²).
Code
PHP Programs
Three starter programs for the right-angled triangle: fixed rows, CLI input, and str_repeat. Use View Output to reveal sample results. Full deep-dives live in each program page.
Example 1 — Fixed rows = 5
Hard-coded height — ideal for first demos and screenshots.
2. Build the row.str_repeat("*", $i) creates a string of length $i filled with stars.
3. Print and advance. Concatenate PHP_EOL so each row ends cleanly.
Learn the two-loop version first (Examples 1–2) so you can explain both bounds in an interview; treat this as a polish shortcut afterward.
Edge Cases & Pitfalls
Check these before calling any star-pattern solution done.
PHP_EOL inside
Column of stars
If echo "*\n" or echo PHP_EOL sits inside the inner loop, each star lands on its own line. Use bare echo "*" for stars; end the row only after the inner loop.
Off-by-one
Wrong height
Use $i = 1; $i <= $rows for 1..rows. Starting at 0 or stopping at < $rows usually breaks the star count.
j to rows
Rectangle, not triangle
Inner bound must be $j <= $i. Looping to $rows every time prints a filled rectangle.
No PHP_EOL
One endless line
Omitting the row break glues every star onto a single line.
rows = 1
Single star
Output is just * on one line — a good sanity check for any pattern.
rows ≤ 0
Empty output
Outer loop never runs. Validate and re-prompt for interactive programs.
Bad input
Check is_numeric
Bare (int) casts turn non-numeric strings into 0 — prefer is_numeric before casting.
Analysis
Time and Space Complexity
Program
Time
Extra space
Nested loops (Examples 1–2)
O(rows²)
O(1)
str_repeat (Example 3)
O(rows²)
O(rows) per temporary row string
Typical series (Programs 1–11)
O(rows²)
O(1) for loop versions
For the starter triangle, total stars = n(n+1)/2. Wider or two-half shapes still print a quadratic number of characters in n.
They print asterisks and spaces in rows to form triangles, pyramids, diamonds, and hollow shapes. Nested for loops plus echo and PHP_EOL are the usual tools.
Start with Program 1 (right-angled triangle). It teaches the outer-row / inner-star idea every later pattern builds on.
The outer loop runs i from 1 to rows. For each row i, the inner loop runs j from 1 to i and prints a star with echo. Row 1 prints 1 star, row 2 prints 2 stars, and so on.
echo "*" stays on the same line with no newline. echo PHP_EOL ends the current line. Stars use echo; the row break uses echo PHP_EOL after the inner loop.
Yes. echo str_repeat("*", $i) . PHP_EOL prints a full row in one call. Nested loops are better for learning; str_repeat is a handy shortcut later.
Most print O(n²) characters for n rows. The starter triangle prints n(n+1)/2 stars; wider or two-half shapes stay quadratic in n.
Use trim(fgets(STDIN)), check is_numeric($input), then cast to int and require rows >= 1.
Add a space loop before the star loop for centering, then mirror the upper half for inverted or diamond shapes. See Programs 5–11.
🤔
Did you know?
Almost every star pattern is the same idea: an outer loop picks the row, then inner loops decide spaces and stars. Master the right-angled triangle first — the rest reuse that skeleton.