PHP 101 (Part 1) - Secret Agent Man - The Toy Shop
(Page 4 of 5 )
Once you've got the basics of variables down, it's time to start doing something with them. We'll start with PHP's most useful mathematical operators - here's an example which demonstrates them:
<?php
// set up some variables
// the toys
$item1 = "X-ray specs";
$item2 = "Watch with built-in poison gas canister";
$item3 = "Exploding chewing gum";
// the price
$item1_cost = 100;
$item2_cost = 250;
$item3_cost = 32;
// the amount
$item1_qty = 1;
$item2_qty = 2;
$item3_qty = 15;
// calculate cost for each item
$item1_total = $item1_cost * $item1_qty;
$item2_total = $item2_cost * $item2_qty;
$item3_total = $item3_cost * $item3_qty;
// calculate grand total
$grand_total = $item1_total + $item2_total + $item3_total;
// special secret agent discount - 10%
$discount = 10;
// which reduces total bill amount
$amount = ($grand_total * 10)/100;
// the bottom line
$net_total = $grand_total - $amount;
?>
<html>
<head>
<title>Boys And Their Toys</title>
<style type="text/css">
td {font-family:Verdana;}
</style>
</head>
<body>
<center>
<table border="3" cellspacing="0" cellpadding="4">
<tr>
<td colspan="4" align="center" valign="middle">
<b>The Secret Agent Store</b>
</td>
</tr>
<tr>
<td>
<b>Description</b>
</td>
<td>
<b>Unit Cost<b>
</td>
<td>
<b>Quantity</b>
</td>
<td>
<b>Total</b>
</td>
</tr>
<?php
// Start echo the values
echo "<tr>
<td>
$item1
</td>
<td align=right>
$$item1_cost
</td>
<td align=right>
$item1_qty
</td>
<td align=right>
$$item1_total
</td>
</tr>";
echo "<tr>
<td>
$item2
</td>
<td align=right>
$$item2_cost
</td>
<td align=right>
$item2_qty
</td>
<td align=right>
$$item2_total
</td>
</tr>";
echo "<tr>
<td>
$item3
</td>
<td align=right>
$$item3_cost
</td>
<td align=right>
$item3_qty
</td>
<td align=right>
$$item3_total
</td>
</tr>";
echo "<tr>
<td colspan=3 align=right>
<b>Gross</b>
</td>
<td align=right>
<b>$$grand_total</b>
</td>
</tr>";
echo "<tr>
<td colspan=3 align=right>
<b>Less discount [$discount%]</b>
</td>
<td align=right>
<b>$$amount</b>
</td>
</tr>";
echo "<tr>
<td colspan=3 align=right>
<b>Net</b>
</td>
<td align=right>
<b>$$net_total</b>
</td>
</tr>";
?>
</table>
<font size=-2 color=silver face=Verdana>Thank you for shopping at the
Secret Agent Store!</font>
</center>
</body>
</html>
Looks complex? Don't be afraid - it's actually pretty simple.
The meat of the script is at the top, where we've set up variables for the various items, the unit cost and the quantity. Next, we've performed a bunch of calculations using PHP's various mathematical operators, and stored the results of those calculations in different variables. The rest of the script is related to the layout and alignment of the various items on the bill, with PHP variables embedded within the HTML code.
Obviously, just as you can add and subtract numbers, PHP also allows you to concatenate strings with the string concatenation operator, represented by a period[.] The next example will make this clear:
<?php
// set up some variables
$a = "Where";
$b = "Are";
$c = "You";
// first alternative
$gamma = $a . " " . $b . " " . $c;
// second alternative
$delta = $a . " " . $c . " " . $b;
?>
<html>
<head>
<title>All Tied Up</title>
</head>
<body>
The first string is <? echo $gamma; ?>
<br>
The second string is <? echo $delta; ?>
</body>
</html>
And here's what you'll see:
The first string is Where Are You
The second string is Where You Are
Next: Weapons To Die For >>
More PHP Articles
More By Vikram Vaswani and Harish Kamath, (c) Melonfire