PHP Basics WorkshopDeveloped by: Paul Burney
OperationsNow we'll take a look at performing some operations on some variables. Most scripts do nothing more complicated than this. To examine these ideas, we'll create an application that calculates a tip for a bill of a given price. First, we'll create the html form for the user to fill in. You can use any editor to do this. Here's the source:
You can see the actual page at tips.html. Let's look at a few of the highlights in this page. The first is the action of this page, tips.php3. That means that the web server is going to send the information contained in this form to a page on the server called tips.php3 which is in the same folder as the form. The names of the input items are also important. PHP will automatically create a variable with that name and set its value equal to the value that is sent. Now we need to create a PHP page that will handle the data. Of course, this page needs to be named tips.php3. The source is listed below. One way, perhaps the best way, to create a PHP page is to create the results page in a graphical editor, highlighting areas where dynamic content should go. You can then use a text editor to replace the highlighted area with PHP.
We'll look at this code a bit more closely, paying most attention to the line numbers that are highlighted. Note that the line numbers are there for illustrative purposes only. Please do not include them in your source code. Lines 11 and 13 check to see if the $sub_total and $tip_percent variables are empty. If they are, they give an error message. Line 15 converts the tip percentage into a decimal that we can multiply by. Line 17 multiplies the tip decimal by the sub total to get the tip. Line 19 gets the total cost by adding the sub total to the tip. Lines 25, 27, 29 and 31 display the PHP variables on the results page. You can now see how the page works at tips.html. |
|||||