PHP
  Home arrow PHP arrow Page 4 - PHP 101 (Part 1) - Secret Agent Man
Dev Shed Forums  
Administration  
AJAX  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Smartphone Development  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Mobile Linux  
App Generation ROI  
IBM® developerWorks  
Forums Sitemap  
E-Commerce Hosting  
Linux Web Hosting  
Managed Hosting  
Small Business Hosting  
VPS Hosting  
Weekly Newsletter

 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid  
Request Media Kit
Contact Us  
Site Map  
Privacy Policy  
Support  
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
PHP

PHP 101 (Part 1) - Secret Agent Man
By: Vikram Vaswani and Harish Kamath, (c) Melonfire
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: starstarstarstarstar / 34
    2000-07-31


    Table of Contents:
  • PHP 101 (Part 1) - Secret Agent Man
  • Bond...James Bond
  • A Case Of Identity
  • The Toy Shop
  • Weapons To Die For

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      error-file:tidyout.log Del.ici.ous error-file:tidyout.log Digg
      error-file:tidyout.log Blink error-file:tidyout.log Simpy
      error-file:tidyout.log Google error-file:tidyout.log Spurl
      error-file:tidyout.log Y! MyWeb error-file:tidyout.log Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article

     
     
    ADVERTISEMENT


    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


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

       

    PHP ARTICLES

    - Using Directory Iterators to Build Loader Ap...
    - Using the spl_autoload() Functions to Build ...
    - Working Out of the Object Context to Build L...
    - Using the _autoload() Magic Function to Buil...
    - The Destruct Magic Function in PHP 5
    - The Autoload Magic Function in PHP 5
    - Developing a Recursive Loading Class for Loa...
    - The Sleep and Wakeup Magic Functions in PHP 5
    - Using the Clone Magic Function in PHP 5
    - Including Files Recursively with Loader Appl...
    - The Call Magic Function in PHP 5
    - Designing a Captcha System with PHP and MySQL
    - Using Static Methods to Build Loader Apps in...
    - The Isset and Unset Magic Functions in PHP 5
    - Advanced PHP Form Input Validation to Check ...





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 3 hosted by Hostway
    Stay green...Green IT