Playing with Numbers

by Varun Thirayan
APDZ.com


<?php

// Set two simple numbers
$n1 = 12;
$n2 = 5;

// Firstly add them
$addition = $n1 + $n2;
// Yep, you guessed it, $addition would be 17!!

// Then we can subtract them
$subtraction = $n1 - $n2;
// $subtration is now equal to 7.

// We can multiply them
$multiplication = $n1 * $n2;
// the answer would now be 60

// And finally we can divide them
$division = $n1 / $n2;
// The result this time would be 2.4

?>


These aren’t the only things that PHP can do with numbers. It has the ability to work out square roots, round numbers to a certain number of decimal places, it can even do trigonometry!! This tutorial won’t be going through all of the functions available though, I’ve never had to use any of these functions in a practical piece of code before. A full list however is available at http://www.php.net – the official home of PHP.

Finally to finish this tutorial, we’ll write a small program probably used in some form by many online shops.

This is how it will run.

1) Take a final price from the user for the products they are purchasing.
2) If it is under £40, add on £7.99 for postage, otherwise don’t add on any money.
3) Finally, add on a tax of 9% of final cost (including any postage) to give the user the amount that they need to pay.

Now, if you’ve been looking for a mini challenge, don’t look at the code below, and go and type it yourself! Otherwise, here’s how it goes.

<?php

// Set the price coming into the code, we’ll assume it came via some type of form.
// You can change this to view the outcome.
$in_price = 27.99;

If ($in_price < 40) {
$new_price = $in_price + 7.99;
} else {
$new_price = $in_price;
}

// As stated in point number 2 above, if the price was less than 40, a postage charge was added.
// Otherwise (else) the new price is simply the price that was entered.

// In this example, $new_price now contains 35.98

// Finally before showing the user, the tax must be added.

$final_price = $new_price + ($new_price * (9 / 100));

// As is taught in school, the contents inside the brackets is worked out first.
// The final price now contains 39.2182
// We want to make this number into an actual currency and tell the user.

Echo “The final sale price for your purchase today is £” . round($final_price, 2);
Echo “<br />Thank you for your service”;

// The round function simply rounds the number to 2 decimal places.
// It’s not necessary but makes sense when working with currency.

?>


Now go back and change the values, try charging someone an outrageous amount for postage to see how the code and your PHP reacts. What about giving a discount if the value is over £200?? And what about actually making it into a form, so that someone can type in an input price to see how much they are going to have to pay.

Hopefully this tutorial has made some sense and as usual, I’ve written wayyyy more than I was planning to… but if you need any help at all, please feel free to post below.

Thanks for reading!!


Options:
Printer Friendly
Email Friend

About The Author:

© Varun Thirayan http://www.apdz.com

Developer Categories



Developer Tutorials
ASP
CGI & Perl
CSS
Flash
HTML
Java
JavaScript
MySQL
PHP
Python
XML

Web Hosting

BlueHost Hosting - Unlimited Bandwidth, Unlimited Disk Space, Unlimited Domains for only $3.95!

Developer Documentation

Developer Tools



Search our Developer Tutorials
  The DevSyndicate Network