PHP Basics Workshop

Developed by: Paul Burney

 
 
 

Sections





<-Prev-    -Next->

Operations

Now 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:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"
"http://www.w3.org/TR/REC-html40/loose.dtd">
<html>
<head>
<title>Tip Calculator</title>
</head>
<body>

<form action="tips.php3" method="get">

<p>Meal Cost: $<input type="text" name="sub_total" value="" size="7"></p>

<p>Tip %: <input type="text" name="tip_percent" value="20" size="3">%</p>

<p><input type="submit" value="Calculate!"></p>

</form>

</body>
</html>

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.

1 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"
2 "http://www.w3.org/TR/REC-html40/loose.dtd">
3 <html>
4 <head>
5 <title>Tip Calculation Complete</title>
6 </head>
7 <body>
8
9 <?php
10
11 if ($sub_total == "") { echo "<h4>Error: You need to input a total!</h4>";}
12
13 if ($tip_percent == "") { echo "<h4>Error: You need to input a tip percentage!</h4>";}
14
15 $tip_decimal = $tip_percent/100;
16
17 $tip = $tip_decimal * $sub_total;
18
19 $total = $sub_total + $tip;
20
21 ?>
22
23 <form action="tips.php3" method="get">
24
25 <p>Meal Cost: <strong>$<?php echo $sub_total; ?></strong></p>
26
27 <p>Tip %: <strong><?php echo $tip_percent; ?>%</strong></p>
28
29 <p>Tip Amount: <strong>$<?php echo $tip; ?></strong></p>
30
31 <p>Total: <font size="+1" color="#990000"><strong>$<?php echo $total; ?></strong></font></p>
32
33 </form>
34
35 </body>
36 </html>

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.