Variables illustration


The current date and time is September 7th, 2010 8:45:AM

Companion Web Site

Larry Ullman has a web site devoted to PHP for the World Wide Web: Second Edition at this location.

It is intended as a supplement /complement to the book and has many helpful resources.

The Book

Variable image

PHP for the World Wide Web: Second Edition written by Larry Ullman

Chapter 4
Numbers

Adding, Subtracting, Multiplying and Dividing

This little widget form below demonstrates the ability of a PHP script to do all these basic operations and produce a total cost of some widgets which will have:

  • price of multiples
  • tax
  • discount
  • choice of delivery

It will then figure how much the buyer would need to pay in monthly installments. This would work well on a shopping cart page.

Your Product Cost Calculator

Price:
Quantity:
Discount:
Tax:
Shipping Method:
Number of Payments to make:

Formatting Numbers

Formatting the numbers with $total = number_format ($total = 2);, the numbers will then be rounded to 2 places. For example, before applying the number formatting, the cost could be something like this :$115.5036 instead of the $115.50 we will see now that the numbers have been formatted to 2 places. This function will also add 0's to a number that is such as $115 so that it becomes $115.00. Another feature is that number_format will add commas to large numbers: 123456789 becomes 123,456,789. It works like a charm!

Another function, round () will do much the same and in addition round a number in this way: $num = 236.26985;

round($num); would return 236.

Using Multiple Operators

Precedence - the order in which operations are performed

There is an order precedence, but memorizing it becomes a difficult task. The whole thing can be solved by using parentheses. Parentheses can make smaller scripts by placing multiple operations on one line.

Incrementing and Decrementing Numbers

Mr. Ullman calls this an ugly construct: $tax = $tax + 1; and a better way is to use multiple operators as in: $var = 20; $var++; is 21; $var++; is 22 and in like fashion, $var --; is 21. So, the handle_calc script is edited again to include this new information.

Operators, when paired with =. will perform the operation on the variable and give the result. For example, if $tax = 5, then $tax/100 = .05.

Creating Random Numbers

The Lucky Numbers Script!

And, Your lucky numbers are:

20, 4, 21

If you don't like these numbers, then reload the page to get new ones.


Showing Source Code

The source code in this document would not show properly. It displayed in 1 or 2 lines. I had noticed that in trying to reconstruct the page, that before reformatting the form with form4.css, the source code did display correctly. Krisse looked over the files, both html and .css. She isolated the code br {display: none;} as the problem. Not only was it formatting the form, but was also working on the show_source function display. It is now removed and the source code displays as it should (except for still the need to cut it manually).

Another note from Krisse, the Spry Menu Bar widget is unnecessary! It causes complaints from the W3C and it now seems that the menu bar functions well without it. Tried it and it does! It too is gone.

 


<?php 
/* My Variables for the study group site */
$book "<u>PHP for the World Wide Web: Second Edition</u>";
$author "Larry Ullman";
$myName "Joanne Johnson";
$pageTitle "PHP Study Group ";

?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title><?php echo $pageTitle ?></title>

<link href="common/ullman.css" rel="stylesheet" type="text/css" />
<link href="../SpryAssets/ullmanMenu.css" rel="stylesheet" type="text/css" />
<link href="/ullmanBook/common/form4.css" rel="stylesheet" type="text/css" />
<script src="../SpryAssets/SpryMenuBar.js" type="text/javascript"></script>



<!--[if IE 7]><link rel="stylesheet" href="common/fieldsetStyleIe.css" 
type="text/css" /><![endif]-->




</head>

<body class="thrColHybHdr">

<div id="container">
  <div id="header">
    <h1><?php echo $pageTitle ?></h1>
    <p>Presented by <?php echo $myName ?><br />
    Started January, 2009<br />
    </p>
    <br />
  <!-- end #header --></div>
  <div id="sidebar1">
    <?php include("includes/nav.php"); ?>
    
  
    <img src="images/var.jpg" 
    alt="Variables illustration" width="140" height="373" />
    <br />
        <br />    
        <br />
    
    <p>The current date and time is <?php echo date ("F jS, Y g:i:A");?></p>
  <!-- end #sidebar1 --></div>
  <div id="sidebar2">
    <h3>Companion Web Site</h3>
    <p>Larry Ullman has a web site devoted to <?php echo $book ?> at this 
    <a href="http://www.dmcinsights.com/phpvqs2/" target="_blank">location</a>.</p>
    <p>It is intended as a supplement /complement to the book and has many 
    helpful resources.</p>
    <ul>
      <li><a href="http://www.dmcinsights.com/phpvqs2/errata.php">Errata</a></li>
      <li><a href="http://www.dmcinsights.com/phorum/list.php?10" target="_blank">Forum</a></li>
      <li><a href="http://blog.dmcinsights.com/" target="_blank">L. Ullman Blog</a></li>
      <li><a href="http://www.dmcinsights.com/bk_pages/faq.php?i=phpvqs2" target="_parent">FAQ</a></li>
      <li><a href="http://de.php.net/manual/en/manual.php" target="_blank">PHP Manual</a></li>
    </ul>
    <p><img src="images/book.jpg" alt="The Book" width="163" height="203" /></p>
    
    <div align="center"><img src="images/var.jpg" alt="Variable image" width="150" height="400" />
      </div>
  <!-- end #sidebar2 --></div>
  <div id="mainContent">
  <center><h3><?php echo $book ?> <span class="spanTitle" >written by</span> 
  <?php echo $author ?></h3></center>
    <h2 align="center">Chapter 4<br />
      Numbers</h2>
    <h3 align="left">Adding, Subtracting, Multiplying and Dividing</h3>
    <p align="left">This little widget form below demonstrates the 
    ability of a PHP script to do all 
    these basic operations and produce a total cost of some widgets 
    which will have:</p>
    <div id="listDiv">
      <ul>
        <li> price of multiples </li>
        <li>tax</li>
        <li>discount</li>
        <li> choice of delivery </li>
      </ul>
    </div>
    <p> It will then figure how much the buyer would need to pay 
    in monthly installments. 
    This would work well on a shopping cart page.</p>
  <div id="formDiv">
     
        <h4 align="center">Your Product Cost Calculator</h4>
        <form action="handle_calc.php" method="post">
          <p class="labels">Price:
        
            <input type="text" name="price" size="5"/> 
            <br />
          Quantity: 
          <input type="text" name="quantity" size="5"/> 
          <br />
          Discount: 
          <input type="text" name="discount" size="5" /> 
          <br />
          Tax: 
          <input type="text" name="tax" size="3" />
          <br />
          Shipping Method: 
          <select name="shipping">
                    <option value="5.00"> Slow and Steady: $5.00 </option>
                    <option value="8.95"> Put a move on it: $8.95 </option>
                    <option value="19.36"> I need it yesterday: $19.36. </option>
            </select>
          <br />
          Number of Payments to make: 
          <input type="text" name="payments" size="3"/> 
          <br />
          <input type="submit" name="submit"  value="Calculate!" class="buttonSubmit"/> 
          <br />
          </p>
      </form>
         </div>
    <h3>
      <!--Script 4.1- calculator.html -->
      
      <!--Script 3.3 -->
      <!--Script 3.3 -->
      <!-- end #mainContent -->
      Formatting Numbers    </h3>
    <p>Formatting the numbers with $total = number_format ($total = 2);, 
    the numbers will then be 
    rounded to 2 places. For example, before applying the number formatting, 
    the cost could be something 
    like this :$<strong>115.5036</strong> instead of the 
    $115.50 we will see now that the numbers have
     been formatted to 2 places. 
     This function will also add 0's to a number that is such as $115 
     so that it becomes $115.00. Another feature is 
     that number_format will add commas to large numbers: 
    123456789 becomes 123,456,789. It works like a charm!</p>
    <p>Another function, round () will do much the same
    and in addition round a number in this way:    
      $num = 236.26985;<br />
      <br />
    round($num); would return 236.</p>
    <h3>Using Multiple Operators<br />
    </h3>
    <p><em>Precedence</em> - the order in which operations are 
    performed</p>
    <p>There is an order precedence, but memorizing it 
    becomes a difficult task. 
    The whole thing can be solved by using parentheses. 
    Parentheses can make 
    smaller scripts by placing multiple operations on one line.</p>
    <h3>Incrementing and Decrementing Numbers</h3>
    <p>Mr. Ullman calls this an ugly 
    construct: <em>$tax = $tax + 1;</em> and 
    a better way is to use multiple 
    operators as in: $var = 20; $var++; is 21; $var++; is 22 
    and in like fashion, $var --; 
    is 21. So, the handle_calc script is edited again 
    to include this new information.</p>
    <p>Operators, when paired with =. will perform the operation 
    on the variable and give the result. 
    For example, if $tax = 5, then $tax/100 = .05.</p>
    <h3>Creating Random Numbers</h3>
    <h4>The Lucky Numbers Script!</h4>
    
    <?php 
    ini_set 
('display_errors'1);
    
error_reporting (E_ALL & ~E_NOTICE);
    
$n1 rand (199);
    
$n2 rand (199);
    
$n3 rand (199);
    
//Echo the numbers
    
echo "<p> And, Your lucky numbers are:</p>
     <pre>$n1, $n2, $n3</pre>"
;
    echo 
"<p>If you don't like these numbers, 
    then reload the page to get new ones.</p>"
;
?>

    <br />
    <h3>Showing Source Code    
    </h3>
    <p>The source code in this document would not show properly. It displayed in 1 or 2 lines. I had noticed that in trying to reconstruct the page, that before reformatting the form with form4.css, the source code did display correctly. Krisse looked over the files, both html and .css. She isolated the code br {display: none;} as the problem. Not only was it formatting the form, but was also working on the show_source function display. It is now removed and the source code displays as it should (except for still the need to cut it manually).</p>
    <p>Another note from Krisse, the Spry Menu Bar widget is unnecessary! It causes complaints from the W3C and it now seems that the menu bar functions well without it. Tried it and it does! It too is gone.</p>
    <p>&nbsp;</p>
    <p><br class="clearfloat" />
     </p>
  </div>
  <div id="footer">
    <p align="center">&copy;R. Joanne Johnson 2009 </p>
    <p align="center"><a href="http://validator.w3.org/check?uri=referer" 
    target="_blank"><img
        src="http://www.w3.org/Icons/valid-xhtml10"
        alt="Valid XHTML 1.0 Transitional" width="88" height="31" border="0" /></a></p>
  <!-- end #footer --></div>
<!-- end #container --></div>
 
<div id="sourceDiv">
   <?php  
   
/* shows the source code of the page */
   
show_source(basename($_SERVER['PHP_SELF'])); ?>
    
  <?php  
   
/* shows source for nav bar include */
  
show_source("includes/nav.php");?>
</div>


</body>
</html>
<ul id="MenuBar1" class="MenuBarVertical">
          <li><a href="/ullmanBook/chapter1.php">Chapter 1</a></li>
      <li><a href="/ullmanBook/chapter2.php">Chapter 2</a></li>
  <li><a href="/ullmanBook/chapter3.php">Chapter 3</a> </li>
  <li><a href="/ullmanBook/chapter4.php">Chapter 4</a></li>
  <li><a href="/ullmanBook/chapter5.php">Chapter 5</a></li>
  <li><a href="/ullmanBook/chapter6.php">Chapter 6</a></li>
      <li><a href="/ullmanBook/chapter7.php">Chapter 7</a></li>
  <li><a href="#">Chapter 8</a></li>
      <li><a href="#">Chapter 9</a></li>
      <li><a href="#">Chapter 10</a></li>
      <li><a href="#">Chapter 11</a></li>
      <li><a href="#">Chapter 12</a></li>
      <li><a href="#">Chapter 13</a></li>
      <li><a href="/ullmanBook/index.php">Home</a></li>
       <li><a href="http://www.lzydaz.com/phpBB3/index.php" target="_blank">Hello World</a></li>
       <li><a href="http://www.lzydaz.com/studygroup/php/index.php" target="_blank">Bonnie</a></li>
       <li><a href="http://www.amaraland.com/studyGroup/index.php" target="_blank">Kc Ladybug</a></li>
       <li><a href="http://krisse.tuna.fi/phpwww/ch1.php" target="_blank">Krisse</a></li>
       
       
    </ul>