Practice 3

Main | Static vs. Dynamic Websites | Using PHP | A Simple PHP Program | Variables and Operators | Practice 1 | Solution 1 | Loops | Practice 2 | Solution 2 | Functions | Practice 3 | Solution 3 | Conditionals | Practice 4 | Solution 4

For this exercise you will redesign the answer to Practice 2 so that the solution uses a function that takes an argument.

Recall our solution to Practice 2.

<html>
I like
<? for ($i=0; $i < 5; $i++) {
        echo "<br>";
} ?>
to space

<?
$i=0;
while ($i < 7) {
        echo "<br>";
        $i++;
} ?>

things
<? for ($i=0; $i < 6; $i++) {
        echo "<br>";
} ?>
out   
</html>

The above code is more elegant than a solution that might have copied and pasted the tag "<br>" several times. However, the above code is still not as neat as it could be, since in order to write it, you probably did some copying and pasting anyway. Write a version of the above that uses a function so that you only have to write your loop statement once. Use a for loop to implement this function.

To complete this exercise you must:

  1. Create a file in "public_html/php" called "space2.php".
  2. Make "space2.php" viewable from the web.
  3. Fill in "space2.php" with this incomplete code.
  4. Complete the PHP section of the incomplete code so that it satisfies the following:
    • Complete spacer by giving it a loop and an echo statement.
    • Make the function take the amount of loops it must do as an argument, $j.
    • Call spacer at the appropriate times.


jfulton [at] member.fsf.org
22 Aug 2013