Some simple PHP exercises to improve your nested FOR loops

This article is intended for PHP novices. It is expected that you know what for loops in PHP are, but have not made much use of them yet. In particular, this article shows how to nest one for loop inside another to create row and column patterns. The best way to see the results of the solutions is by running the solutions on the command line (with php script_name.php). However, you could also run them from a web browser and view source to see the properly formatted output (the output will not look as intended in a web page because the new lines will just show up as spaces).

Exercise 1. The first exercise is trivial but useful as a foundation for the following exercises. The aim is to print 5 lines each consisting of a single asterisk character, '*':

* * * * *

By using a for loop we can specify how many lines we want, and for each line print the character. Here is the solution:

<?php for ($row = 1; $row <= 5; $row++) { echo "*\n"; }

There is just one for loop and we are hard-coding the value of the number of lines, or rows, directly into the for loop instead of setting it up in another variable elsewhere. The advantage of setting up another variable for number of lines is that we could specify the number of lines we want at the top of the program and add logic for calculating the number of columns from it inside the for loop(s). But for this article, I'm hard-coding the value for the sake of simplicity.

In this first example I have a single statement that prints the asterisk and the new line character. In the following exercises this is split up to allow repeating the asterisk character without always following it by a new line. The following exercises also differ from this one in that they have a second, nested for loop used to specify how many asterisks per line to print.

Exercise 2. Use nested for loops to print this pattern:

***** ***** ***** ***** *****

The difference between this pattern and that in exercise 1 is that there are 5 asterisks per line. The pattern could easily be achieved by editing the line in solution 1 that prints the asterisk to

echo "*****\n";

But the point of these exercises is to learn how to use nested for loops. So instead we add a for loop inside the first for loop, this time looping 5 times to print every asterisk, or column:

<?php for ($row = 1; $row <= 5; $row++) { for ($col = 1; $col <= 5; $col++) { echo '*'; } echo "\n"; }

Notice how the asterisk and new line character are now split. Also notice that I have used descriptive variable names, $row and $col, to make it easier to see that we are printing exactly that, rows and columns. I could have opted for $i and $j, but using descriptive names can help clarify what is going on in a program (particularly a large one).

Exercise 3. Use for loops to print this pattern:

* ** *** **** *****

The pattern now adds an extra asterisk per line, starting with 1 asterisk on the first line and ending with 5 on the fifth. This requires a minor change to the previous solution: exchanging the hard coded value of 5 in the inner for loop for $row. This means that for each line, only as many columns as the current line number are printed. The solution:

<?php for ($row = 1; $row <= 5; $row++) { for ($col = 1; $col <= $row; $col++) { echo '*'; } echo "\n"; }

Exercise 4. Use for loops to print this pattern:

***** **** *** ** *

This pattern reverses the one in the previous exercise by starting with 5 asterisks on the first line and ending with 1 on the fifth. However, the number of asterisks on each line still has a relationship to the line number. In this case, if we take the current line number away from 6 (or the max number of lines plus 1) we get the desired number of columns for each line, as each time we are taking away a higher number from 6, starting with 1 (therefore 6 - 1) on the first row and ending with 5 (therefore 6 - 5) on the fifth. The solution:

<?php for ($row = 1; $row <= 5; $row++) { for ($col = 1; $col <= 6 - $row; $col++) { echo '*'; } echo "\n"; }

Exercise 5. Use for loops to print this pattern:

* ** *** ** *

This pattern can be seen as a mixture of the patterns in exercises 3 and 4. The first 3 lines are identical to the pattern in exercise 3, whilst the last 2 are identical to the pattern in exercise 4. My solution uses the ternary operator to decide what relationship to use for the given line (that is, as in exercise 3 versus exercise 4). The ternary operator looks scary at first, but is very simple once you get used to it. What is happening is that the current row number is compared to 3; if it is greater than 3, the same operation as in exercise 4 is carried out to obtain the number of columns, otherwise the same operation as in exercise 3 is used. The solution:

<?php for ($row = 1; $row <= 5; $row++) { for ($col = 1; $col <= ($row > 3 ? 6 - $row : $row); $col++) { echo '*'; } echo "\n"; }