| home / programming / php / corephp / 2 | [previous][next] |
|
|
/*
** print message about lunch cost
*/
print("$YourName, you will be out ");
print($CostOfLunch * $DaysBuyingLunch);
print(" dollars this week.< br>\n");
?>
</body>
</html>
The third and fourth assignments are putting numerical data into variables. The value 3.5 is a floating-point, or fractional, number. PHP calls this type a double, showing some of its C heritage. The value 4 in the next assignment is an integer, or whole number.
After printing some HTML code, another PHP code block is opened. First the script prints today's date as a level-three header. Notice that the script passes some new types of information to the print function. You can give string literals or string variables to print, and they will be sent to the browser.
When it comes to variables, PHP is not so lenient with case. Today and today are two different variables. Since PHP doesn't require you to declare variables before you use them, you can accidentally type today when you mean Today and no error will be generated by default. If variables are unexpectedly empty, check your case. You can also catch these sorts of errors by configuring PHP to warn you of uninitialized variables. See Chapter 15's description of error reporting.
The script next prints Leon, you will be out 14 dollars this week. The line that prints the total has to calculate it with multiplication, using the * operator.
Manipulating variables that you set within your script is somewhat interesting, but hardly anything to rave about. Scripts become much more useful when they use input from the user. When you call PHP from an HTML form, the form fields are turned into variables. Listing 1.9 is a form that calls Listing 1. 10, a further modifica-tion of our example script.
Listing 1.9 is a standard HTML form. If you have dealt at all with CGIs, it will look familiar. There are three form fields that match up with the variables from our previous example. Instead of simply putting data into the variables, we will provide a form and use the information the user types. When the user presses the submit button, the script named in the ACTION attribute will receive the three form fields, and PHP will convert them into variables (see Figure 1. 3).
| home / programming / php / corephp / 2 | [previous][next] |
Created: March 27, 2003
Revised: Sept 1, 2003
URL: http://webreference.com/programming/php/corephp/2