Home arrow PHP arrow Page 4 - PHP 101 (Part 1) - Secret Agent Man

The Toy Shop - PHP

PHP is the hottest scripting language around - and with the release of PHP4, more and more developers are looking at it as a rapid Web development tool. This new series of tutorials is aimed at getting novice programmers up to speed on the language, and the first article covers variables, operators and the include() function call. Exploding chewing-gum is optional.

  1. PHP 101 (Part 1) - Secret Agent Man
  2. Bond...James Bond
  3. A Case Of Identity
  4. The Toy Shop
  5. Weapons To Die For
By: Vikram Vaswani and Harish Kamath, (c) Melonfire
Rating: starstarstarstarstar / 36
July 31, 2000

print this article


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

>>> More PHP Articles          >>> More By Vikram Vaswani and Harish Kamath, (c) Melonfire

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort


- Hackers Compromise PHP Sites to Launch Attac...
- Red Hat, Zend Form OpenShift PaaS Alliance
- PHP IDE News
- BCD, Zend Extend PHP Partnership
- PHP FAQ Highlight
- PHP Creator Didn't Set Out to Create a Langu...
- PHP Trends Revealed in Zend Study
- PHP: Best Methods for Running Scheduled Jobs
- PHP Array Functions: array_change_key_case
- PHP array_combine Function
- PHP array_chunk Function
- PHP Closures as View Helpers: Lazy-Loading F...
- Using PHP Closures as View Helpers
- PHP File and Operating System Program Execut...
- PHP: Effects of Wrapping Code in Class Const...

Developer Shed Affiliates


Dev Shed Tutorial Topics: