Skip navigation.
Work out your logic on paper

Chapter 7 Hints and Help

QUESTION: I'm confused when and how to use curly braces in my IF and IF..ELSE structures..

ANSWER: Here's a short explanation of how to use curly braces, including some examples.

QUESTION: When do I use the ENDIF statement in my PHP code?

ANSWER: You don't. The ENDIF statement is just used in the pseudocode examples in the textbook, to show where the IF..ELSE structure ends. ENDIF is not part of the PHP language so you don't use it in your actual code. Instead use { and } braces to indicate the beginning and end of the statements in your IF and ELSE sections. The braces are not required if the IF or ELSE section only contains a single statement.

QUESTION: Now we're using IF..ELSE structures, I'm just having general problems getting my code work. How can I figure out my errors?

ANSWER: Here are some key things to remember with IF..ELSE structures:

  • Always remember to use == and not = when comparing value to see if they are equal. If you use = you are ASSIGNING a value not comparing values.
  • Always consider the order of your instructions. For example you cannot calculate the total cost of an order if this includes shipping and handling before you calculate the shipping and handling cost.
  • Always remember to use braces { and } when you need multiple statements in the IF section or ELSE sections of your code.
  • Always consider carefully which statements need to located inside your IF and ELSE sections. If you see see duplicate code in your IF and ELSE sections that suggests there is a better way to organize your code, perhaps some of the statements can be moved before or after your IF..ELSE section.
  • Refer to Appendix D in the textbook for general debugging help.
  • It also helps to walk through your code one line at a time with a pencil and paper. This is often called "desk-checking". List your variables and make a note of what values are assigned to each variable. Also note what HTML is produced by your print statements. Do exactly what your code says, not what you think it should do. This can help to figure out what might be going wrong.

QUESTION: In Modify3 for example, if ($capital = "cairo") isn't working! What am I doing wrong?

ANSWER: First remember that = ASSIGNS a value to a variable whereas == TESTS whether a variable contains a specific value.

Second remember PHP is case sensitive. So if you are testing for "Cairo", your test must be:

if ($capital == "Cairo")

Be careful to use == instead of = in all exercises where you are testing whether two values are equal to each other. Also, although the quotes around "Cairo" are not always required it's good practice to always use these, in case there are spaces in the character string being tested . The use of quotes around character strings is required by most languages.

QUESTION: I'm not sure what to do,, can you walk me through a Modify exercise?

ANSWER: In Modify1, you are asked to create an IF.. ELSE structure. If the user enters an age less than 65 you must calculate the years to retire:

$yearsToRetire = 65 - $age;
print("<p>Your age is $age. You have $yearsToRetire years until retirement.</p>");

or otherwise calculate the years already retired:

$yearsRetired = $age - 65;
print("<p>You have been retired $yearsRetired years.</p>");

These statements need to be placed inside the IF and ELSE sections of an IF structure that tests if the age is less than 65. Any section that contains more than a single statement must be enclosed in { and } braces. In this case BOTH sections contain 2 statements so BOTH sections must be enclosed in { and } braces, so the solution is:

if ($age < 65)
{
   $yearsToRetire = 65 - $age;
   print("<p>Your age is $age. You have $yearsToRetire years until retirement.</p>");
}
else
{
   $yearsRetired = $age - 65;
   print("<p>You have been retired $yearsRetired years.</p>");
}