This lab is designed as a follow-up to the lab on arrays, and assumes that you have already completed the activities in that lab. If you have not yet finished that lab, you should start there. When you finish it, save a copy of your solution as register0.php. You may then continue working with register.php on this lab.
Using Functions
At a number of points in the previous lab, you were asked to print out an array in the form of a list, or an associative array in tabular form. You probably achieved this by copying the foreach loop in each case. It would be better to capture the similar code in a function, defined once, and called as needed.
Begin by defining a function called array_print_ul that emits the content of an array, with each element in a separate list item. (It should also emit the opening and closing ul tags.) The array should be passed into the function as an argument. When you are finished with the function definition, call the function to display the array of names. Sort the names in reverse order and call it again. What happens if you call it on the array of entrants, which is an associative array?
For a slightly more complex challenge, write another function called array_print_table that prints an associative array as a two-column table. The function should take three arguments: the array itself, and two strings for the headers of the table columns. The latter two should have default values of key and value in case the caller declines to specify them. Use your function to emit out the array of entrants. Then call it again to emit the count of entrants' belt levels. Try calling it the second time without providing column headings.
Functions With Return Values
Sometimes it can be useful to have a function that computes a value and returns it. In this particular example, it would be nice to be able to create an array of the names of entrants with a particular belt color. Write a function called entrants_by_belt that takes the list of entrants and a target belt color for its arguments. It should loop through the entrants array, and each time it finds an entrant of the desired belt color it will add that person's name to a new array being grown inside the function. At the end it will pass back the new array using a return command. Try out this new function in combination with your older ones, to extract and print out the names of all the white belts. Then do the same for the yellow belts.