Variables illustration


The current date and time is February 4th, 2012 2:16:PM

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:

31, 50, 63

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.

 



Warning: show_source() has been disabled for security reasons in /home/rjoannej/public_html/ullmanBook/chapter4.php on line 204

Warning: show_source() has been disabled for security reasons in /home/rjoannej/public_html/ullmanBook/chapter4.php on line 208