It reuses Program 3’s spacing idea, but uses odd star counts so the shape widens on both sides. The same row formula is the upper half of the filled diamond.
Approach
How to Solve It
Two inner loops per row — spaces then odd stars — or the same formulas with str_repeat.
Method
Idea
Best for
Nested loops
$rows - $i spaces, then 2 * $i - 1 stars
Learning, interviews, exams
str_repeat
Build spaces and stars as whole strings
Shorter demos once loops click
Pseudocode
Pseudocode
for i from 1 to rows:
for j from 1 to (rows - i):
echo " "
for k from 1 to (2 * i - 1):
echo "*"
echo PHP_EOL
2. Build the row.str_repeat(" ", $rows - $i) for padding; str_repeat("*", 2 * $i - 1) for the odd star run.
3. Learn loops first. Use Examples 1–2 when you need to show nested bounds; treat this as a polish shortcut afterward.
Edge Cases & Pitfalls
Check these before calling the solution done.
2 * $i
Even width
Use 2 * $i - 1 (odd). Even widths lose the classic single-center peak.
$j < $rows - $i
One space short
Space loop must be $j <= $rows - $i. A strict < shifts the peak.
Only $i stars
Program 3 shape
Same spaces with $i stars is the right-aligned triangle, not a full pyramid.
PHP_EOL inside
Column of characters
If echo PHP_EOL is inside either inner loop, each character lands on its own line. Print cells with echo; end the row only after both loops.
$rows = 1
Single star
0 spaces + 1 star — a good tip sanity check.
Bad input
Check is_numeric
Letters or empty input fail is_numeric — validate before casting to int.
Analysis
Time and Space Complexity
Program
Time
Extra space
Nested loops (Examples 1–2)
O($rows²)
O(1)
str_repeat shortcut (Example 3)
O($rows²)
O($rows) per temporary row string
Each of n rows prints Θ(n) characters (spaces + stars). Total stars = 1 + 3 + … + (2n - 1) = n².
Remember
Key Takeaways
Formulas:$rows - $i spaces and 2 * $i - 1 stars.
Odd widths: keep 2 * $i - 1 for a single centered peak.
Break the row:echo for spaces/stars; echo PHP_EOL after both loops.
Complexity:O(n²) time; total stars = n²; O(1) extra space for nested loops.
One line: print $rows - $i spaces, then 2 * $i - 1 stars — that is the centered pyramid.
Frequently Asked Questions
2*i-1 gives odd lengths 1, 3, 5, … so each row adds one star on both sides. Using only i stars per row would not form the usual symmetric centered pyramid.
Printing (rows - i) spaces before the stars shifts the star block left as i grows, keeping the peak centered when the font is fixed-width.
Yes. Keep the same inner loops but run the outer loop from rows down to 1. The first printed row is the widest; later rows narrow toward the tip. See Program 6.
The last row has 2 * rows - 1 stars and no leading spaces when i equals rows.
echo sends output without adding a line break. PHP_EOL ends the current line with a portable newline. Spaces and stars use echo; the row break uses echo PHP_EOL after both inner loops.
O(n²) for n rows. Each row prints Theta(n) characters in the worst case; there are n rows. Total stars equal n².
Program 3 uses the same (rows - i) spaces but only i stars. Program 5 uses 2*i-1 stars so the shape widens on both sides.
Yes. echo str_repeat(" ", $rows - $i) . str_repeat("*", 2 * $i - 1) . PHP_EOL builds each row without explicit inner character loops.
🤔
Did you know?
Odd star counts 1, 3, 5, … come from 2 * i - 1. Their sum for n rows is n² — so total stars grow as a perfect square. This pyramid is also the upper half of the filled diamond.