Skip navigation.
Experiment, try things out, have fun learning

Chapter 5 Hints and Help

QUESTION: When I view my files in a Web browser I see the PHP code instead of the results, or I see nothing at all. What am I doing wrong??

ANSWER: Now that you are working with PHP, you can't just double-click your HTML or PHP files in your File Manager to view them. Doing that will not connect to the Web server even if it is running and so you will probably just see PHP code. This is explained in the book on pages 32 and 33. Instead you must: (1) run the Web server and then (2) use the URL beginning http://localhost to connect to the Web server and view your file, for example:

http://localhost/webtech/coursework/chapter05/fixit2.php

The same is true when you want to connect to an HTML forms that will run a PHP files, for example:

http://localhost/webtech/coursework/chapter05/modify1.html

If you're not sure whether you are connecting to the Web server, Look in your Web browser's address box. If the address begins file://instead of http://localhost it means you are not connecting to the server (you probably just double clicked a file to view it). Just change the address to the correct URL beginning http://localhost. Read page 34 in the book which reviews these steps in more detail.

QUESTION: I'm not sure what to do with the fixit exercises, help!

ANSWER: Each fixit file contains a problem and you are asked to fix it. Read the comment section at the start of the Fixit file to get an idea of what the problem is. You can view the file in a Web browser to see the problem, then use your text editor to fix it.

For example, start your Web server and view fixit1.php (the address is http://localhost/webtech/coursework/chapter05/fixit1.php). As you will see, the browser displays the first three assignment statements and then displays inaccurate results. Now examine fixit1.php in your text editor and you will see that the <php tag appears in the wrong location. It should appear before the three assignments statements since these are PHP statements. So move the <php tag before those statements, save the file, and view it again in your Web browser. It should now display correctly.

QUESTION: I'm not sure what to do with the modify exercises, help!

ANSWER: In the modify exercises, you are making small changes in your modify code to meet the requirements. Read the comment section at the start of the modify file to find out what you are expected to do. For example in modify1, you are asked to also calculate and display the number of years the person has worked, based on their age and assuming they started work at age 18.

You don't need to change the modify1.html page since this already asks the user for their age and that's all you need to calculate the years worked. So you just need to change modify1.php by adding a statement that will calculate the years worked so far and store this in a variable, then add a statement to display this.. So here is the correct code:

<?php
$age = $_POST['age'];
$yearsToRetire = 65 - $age;
$yearsWorked = $age - 18;

print("<p>Your age is $age. You have $yearsToRetire years until retirement.</p>");
print("<p>You have worked for $yearsWorked years.</p>");
?>

Since you are now writing an actual program, your code must be the correct PHP syntax, as described in Chapter 5.

In modify2, you are asked to change the application so the user is asked for the age they plan to retire as well as their age, and to calculate how many years they have left to work based on the age they plan to retire instead of 65. So that means you will need to add an extra input to your form in modify2.html to ask the user for the age they want to retire. Be sure to give it a meaningful name, for example ageToRetire. Then you must change your modify2.php code to receive this additional input and store it in a variable.

QUESTION: I modified an HTML file or PHP program but when I try to view it I still see the previous version in my Web browser. Why don't my changes show up???

ANSWER: First check that you saved your changes before you tried to view the modified file! Second, if you're viewing an HTML file be sure to refresh your Web browser window to view the latest verson of the file, otherwise the previous version may be displayed.

If neither of these works, perhaps your modified file was saved with a different name or in a different folder location, and you are still viewing the orginal file in your browser. Be sure to save your modified file with the same name in the original folder location (for example if you modify fixit1.html in the coursework/chapter04 folder, be sure to save your modified file as fixit.html in the coursework/chapter04 folder).

The last possibility is that the Web browser is in fact showing your file but your changes contain errors so it is not displayng what you expect. To test this, try making an obvious and simple change (for example add XXX to an HTML heading in the file) and see if the XXX appears when you view the file in your browser window. If it does, it means you are seeing the latest version of your file so there is a code error that you will need to figure out.

QUESTION: I submitted my HTML form but the results appeared to come from the wrong PHP program! What's going on?

ANSWER: Check the action attribute in your <form> tag. You probably pasted an incorrect PHP file name here, or else mistyped the file name.

QUESTION: I don't understand the statements that use $_POST. What exactly are these statements for?

ANSWER: These statements can be confusing but basically $_POST is a structure called an array that contains the inputs submitted from the form. We will learn mcu more $_POST in Chapter 12. Each of the inputs in the form has a name, for example the names of the inputs for the operating system and number of copies in the form in softwareOrder.html are "os" and "numCopies", so your PHP program refers to $_POST ['os'] and $_POST ['numCopies'] to receive these values. The two values are then assigned to program variables, in this case $os and $numCopies:

$os= $_POST ['os'];
$numCopies = $_POST ['numCopies'];

Now these variables contain the same values that the user submitted and you can use them in subsequent program statements. You could actually refer directly to $_POST ['os'] and $_POST ['numCopies'] throughout your program but it is usually simpler to work with ordinary variables like $os and $numCopies.

Note that $os and $numCopies are not required to have the same names as the names in the form, for example you could use:

$operatingSystem= $_POST ['os'];

But you must still refer to 'os' in your $_POST array since that references the name used in the form. And of course you must now use $operatingSystem and not $os in subsequent program statements.

QUESTION: The round() and number_format() functions: What's the difference? How do I display numbers rounded to two decimal places?

ANSWER: The round() function is an arithmetic function. It receives a value as the first argument and rounds this number to the number of decimal places specified by the second argument. For example:

round (12.347, 2) returns 12.35
round (12.1, 2) returns 12.1 (not 12.10 since this is an arithmetic function, and 12.1 is the same numeric value as 12.10)
round (12.001, 2) returns 12 (not 12.00 since 12 is the same numeric value as 12.00)

Using round() is appropriate when you want a numeric value (for calculations) but what if you want to display the value so that it will always display a specified number of decimal places, for example, to display currency which should always display two decimals?

To display a value rounded to any number of places you use the number_format() function. This is a string function. Instead of returning a numeric value it returns a string containing the number with the required number of decimal places. For example:

number_format (12.347, 2) returns 12.35
number_format (12.1, 2) returns 12.10
number_format (12.001, 2) returns 12.00

So use number_format() if you want to display a value to a certain number of places. Use round() when you want to calculate a number to a certain number of places.

QUESTION: Do I really need to use different print statements for each HTML paragraph or other text. Can't I just use a single print statement where your examples show two or more print statements one after the other?

ANSWER: Absolutely! In fact it's a bit more efficient to use a single print statement rather than using multiple print statements one after other. The only reason for using multipple print statements in the book is to help beginners understand the code more clearly by separating things out.

QUESTION: How do I distinguish between singular and plural in my output? For example if someone enters 1 in a form for the number of travelers, I want to display "1 person" in my response, but if they enter 2, I want to display "2 people"

ANSWER: Good question! You can handle these issues using selection statements. We'll cover selection in chapter 7, but here's an example:

if ($numTravelers > 1)
   print ("There are $numTravelers people.");
else
   print ("There is $numTravelers person.");