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  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Forums Sitemap 
IBM® developerWorks 
Sun Developer Network 
Dedicated Servers 
E-Commerce Hosting 
Linux Web Hosting 
Managed Hosting 
Small Business Hosting 
Moblin 
JMSL Numerical Library 
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: 5 stars5 stars5 stars5 stars5 stars / 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:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb 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

    - Validating Web Forms with the Code Igniter P...
    - Output Buffering
    - Paginating Database Records with the Code Ig...
    - HTTP Headers in Web Development
    - Project Management: Administration
    - Building a Database-Driven Application with ...
    - User Authentication for a Project Management...
    - Introduction to the CodeIgniter PHP Framework
    - Adding Users for a Project Management Applic...
    - Migrating Class Code for a MIME Email to PHP...
    - Login and Logout Authentication for a Projec...
    - Composing Messages in HTML for MIME Email wi...
    - Project Management: Authentication
    - A Better Way to Determine MIME Types for MIM...
    - Project Management Overview





    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 3 hosted by Hostway