Skip navigation.
Allow sufficient time for real learning

Chapter 8 Hints and Help

QUESTION: The wage7 application in the samples folder does not seem to work correctly. If I enter 0 for either the hours worked or hourly wage, the application returns a message indicating no input. A test such as

   if (empty($hoursWorked))

is supposed to return TRUE if the variable does not contain a value but it looks as if the test also returns TRUE if the variable contains 0!

ANSWER: Good observation! The problem is that the PHP empty() function also returns true if the variable contains 0 (or FALSE). So the correct code would be:

   if ( ($hourlyWage != 0 AND empty($hourlyWage)) OR ($hoursWorked != 0 AND empty($hoursWorked)) )
      print("ERROR: Input is missing!");

If you make this change the code will work correctly.

QUESTION: I'm not sure how to answer the quiz questions that ask about the number of paths in a piece of code.

ANSWER: Each time you run an application that contains IF or IF..ELSE structures, the execution may take a different path through the code instructions depending on whether or not each test is true or false. Counting the number of possible paths helps you to determine how many tests you must perform to ensure that the application is working correctly (you will want at least one test for each possible path through the code).

In this book we keep it simple. The chapter review questions might ask how many different paths are there through a program that contains a single IF statement or a single IF..ELSE statement, or a combination of these, for example an IF statement followed by an IF..ELSE statement?

We know that there are two paths through an IF statement (the IF section executes if the test is true, and the program skips this section if the test is false), and there are also two paths through an IF..ELSE statement (the IF section executes if the test is true, the ELSE section executes if the test is false). So in either case there are two paths, True or False.

If a program contains an IF statement followed by an IF..ELSE statement, then there are four paths:

   The first test may be true and the second test may also be true
   The first test may be true and the second test may be false
   The first test may be false and the second test may be true
   The first test may be false and the second test may also be false

Each of these possibilities means that different instructions will be executed, so there are four possible paths through the program.

QUESTION: Can you explain the difference between separate, chained and nested IF..ELSE structures?

ANSWER: Separate structures are ones where each structure is unrelated to the other. For example:

if ($hourlyWage < 8.00)
   $hourlyWage = 8.00;

is a completely separate test than

if ($hoursWorked >=35)
   $bonus = 50.00;
   $bonus = 25.00;

They are handling different problems and what happens in one of these tests is not related to what happens in the other. So even if thesee tests follow each other in the code,m they are independent of one another.

On the other hand a chained group of IF..ELSE structures ARE related to each other. These are oftem used to provide more than two possible results for the same test. For example:

if ($hoursWorked >=35)
   $bonus = 50.00;
else if ($hoursWorked >=20)
   $bonus = 25.00;
   $bonus = 10.00;

In this case the second IF ELSE is attached (chained) to the ELSE of the first IF ELSE. So the second IF ELSE is only ever considered if the first IF test is FALSE. If the first test is TRUE, the ELSE is skipped and this ELSE contains the second IF ELSE. So in this case there are two structures but the second is connected to the first to produce three options. This is quite common since we often have more than two options to handle. PHP allows you to use an ELSEIF key word to simplify typing these out:

if ($hoursWorked >=35)
   $bonus = 50.00;
elseif ($hoursWorked >=20)
   $bonus = 25.00;
   $bonus = 10.00;

Here's another. lengthier, example to print a month name based on the month number:

if ($month == 1)
elseif ($month == 2)
elseif ($month == 3)
elseif ($month == 4)
elseif ($month == 5)
elseif ($month == 6)
elseif ($month == 7)
elseif ($month == 8)
elseif ($month == 9)
elseif ($month == 10)
elseif ($month == 11)
elseif ($month == 12)

These exampleS contain a single statement for each IF and ELSE block, but you could have as many statements as needed in each block as long as you use curly braces around them.

The third option is to use NESTED IF..ELSE structures, where some are nested inside others. For example, we might test for a password and only if the password is correct do we process some input. Processing that input may require other IF..ELSE structures that would be nested inside the first:

if ($password == "xyz")
   if ($hoursWorked >= 35)
      $bonus = 50.00;
      $bonus = 25.00;
   print ("<p>You get a $bonus bonus!</p>");
   print ("<p>ERROR! Wrong password!!!</p>");

In this case the 2nd IF ELSE is nested inside the IF section of the 1st IF ELSE so it will only be tested if the 1st test is true. Note the use of braces to indicate the five lines that are part of the first IF section.

The decision which structure to use is entirely based on your program requirements. It's very helpful to work out your algorithms on paper before you start coding. As you gain experience, you will find that this becomes much easier.