Solution Review: Printing Pyramid
In this lesson, we'll look at the explanation of the last challenge.
We'll cover the following
Let’s look at the solution first before jumping into the explanation:
$rows = 5;for($i = 1; $i <= $rows; $i++){for($j = 1; $j <= $i; $j++){print "a ";}print "\n";}
Explanation
The outer for
loop executes $rows
times, and the inner for
loop executes once for the first iteration of outer loop, twice for the second iteration of the outer loop, and so on. The inner loop prints an a
character on the console every time it executes. At the end of the inner loop, it prints a \n
to get us on a new line.