A V-shaped hollow pattern prints only the outline of an upright V: two stars on the widest top row, then legs that meet at a single bottom vertex.
Remember
Rule: same as Program 7 — only reverse the outer loop
* *
* *
* *
* *
* ← 5 rows (width 9)
It is the flip of Program 7: keep the same left/right conditions, reverse only the outer loop. That mirrors how Program 6 inverts Program 5. This outline is also the lower half of the hollow diamond.
Approach
How to Solve It
Two ways to emit the same outline — start with if/else legs, then optionally shorten with a ternary.
Method
Idea
Best for
If/else legs
Countdown $i; left $j and right $k; star when indices match
Learning, interviews, exams
Ternary ? :
Same bounds; one-line star-vs-space choice
Shorter demos once conditions click
Pseudocode
Pseudocode
for i from rows down to 1:
for j from rows down to 1:
print "*" if i == j else " "
for k from 2 to rows:
print "*" if i == k else " "
print newline
1. Same countdown. Still walk $i from $rows down to 1.
2. Same bounds. Left $j still counts down; right $k still starts at 2.
3. Shorter print.$i == $j ? "*" : " " replaces the multi-line if/else — same decision, less code.
Learn the if/else version first (Examples 1–2) so you can explain the branch in an interview; treat this as a polish shortcut afterward.
Edge Cases & Pitfalls
Check these before calling the solution done.
$i++
Inverted V by mistake
If you increment $i from 1 to $rows, you reprint Program 7. Use $i-- from $rows down to 1.
$k = 1
Duplicate vertex
Starting the right loop at $k = 1 prints two stars on the bottom row. Keep $k = 2.
PHP_EOL inside
Broken outline
If echo PHP_EOL is inside either inner loop, each cell lands on its own line. Use bare echo for cells; end the row only after both loops.
rows = 1
Single vertex
Output is just * — right loop never runs. A good sanity check.
rows ≤ 0
Empty output
Outer loop never runs. Validate and re-prompt for interactive programs.
Bad input
Use is_numeric
Bare (int) casts turn letters into 0 — prefer is_numeric and require $rows >= 1.
Analysis
Time and Space Complexity
Program
Time
Extra space
If/else legs (Examples 1–2)
O(rows²)
O(1)
Ternary form (Example 3)
O(rows²)
O(1)
About n rows × 2n - 1 characters printed per row — still quadratic in n. Total stars = 2n - 1 (same as Program 7; only print order differs).
Remember
Key Takeaways
Rule: countdown $i; print * only when $i == $j or $i == $k.
Flip of Program 7: same inner loops — only reverse the outer loop.
Break the row: call echo PHP_EOL only after both inner loops.
Complexity:O(n²) time; O(1) extra space.
One line: for $i from $rows down to 1, print a star only when the left or right index matches $i — start the right loop at 2.
Frequently Asked Questions
Program 7 runs i from 1 to rows (inverted V: narrow top). Program 8 runs i from rows down to 1 with the same inner loops, so the first line uses i equal rows and prints stars at both outer columns. As i decreases, both legs move inward until the last line has a single bottom vertex.
Only the outer loop direction changes. Program 7 uses i from 1 to rows. Program 8 uses i from rows to 1. The conditions i equals j and i equals k are the same.
When i is 1, the left loop still prints a star at j equals 1. The right loop runs k from 2 to rows, so i equals k never holds on that row.
echo stays on the same line. echo PHP_EOL ends the current line. Stars and spaces use echo; the row break uses echo PHP_EOL after both inner loops.
Each line has width 2 * rows - 1 characters — same geometry as Program 7, only the row order is reversed.
This page is the lower half of Program 9. Stack Program 7 on top, then this body from rows-1 down to 1, to complete the diamond.
O(n²) for n rows. Each row runs Theta(n) iterations across the two inner loops.
Use trim(fgets(STDIN)) and check is_numeric($input) before casting to int so bad input does not produce unexpected results.
🤔
Did you know?
This hollow V is exactly Program 7 with the outer loop reversed — the same trick as Program 5 versus Program 6. It is the lower half of the hollow diamond. The bottom vertex is a single star because $k starts at 2.