Playing with Numbers

by Varun Thirayan

In this tutorial, we’ll do some mathematical functions with numbers and also make use of the ‘if’ statement.

In an earlier tutorial, I described how to define variables as numbers. Basically you chose a variable name, for this example lets chose ‘$number’, and you chose a value, we’ll use 15. The code could then be written as shown below.

<?php

$number = 15;

echo $number;

?>


If you save and run this file as a .php, this will make the variable $number equal to 15 and then print it on the page… I’m sure you knew that already... So what else can we do with numbers?

PHP’s brain has a lovely in built calculator that allows it to carry out simple (and much more complicated) functions with numbers. As always, the best way to see this in action is in an example.

(Just so that you know, in any PHP code, text after a ‘//’ is ignored. This is useful for commenting work for future reference and also to explain on the job, as shown below)

<?php

// Firstly set 3 different variables with three different values
$n1 = 12;
$n2 = 16;
$n3 = 3;

// We can add these numbers to make a new variable
$new_variable = $n1 + $n2 + $n3;

// The $new_variable now has the value ‘31’. Let’s show this:

echo “The new variable is now equal to “ . $new_variable;

// The ‘echo’ command and the way is works
// was explained in a previous tutorial.

// We can also just increment (increase by 1) the value of a variable.

$n1 = $n1 + 1;

// The above line literally says, ‘the new value of n1 equals old n1 plus 1’
// if we were to echo $n1 now, it would display 13. Try it for yourself.

// There is a shorthand, quicker and easier way of doing this.
// Taking into account that $n1 is now equal to 13, the below line
// would make $n1 equal to 14.

$n1++;

// Simple… right? $ni now contains 14.
// A variable with ‘++;’ after it, means it will increase by one.

?>


Was that a bit too heavy? Read it again. Razz

It’s complicated for a first time user but it is also logical. When reading lines of code, say it into yourself in words so that it makes more sense (don’t say it aloud, you’ll look weird).

The short-hand addition line is used a lot and it also works for the negative. Eg:

<?php

$n1 = 12;

// First way to minus one:
$n1 = $n1 – 1;

// and the shorter way:
$n1--;

?>


It’s easy to see why it’s used, it’s just much simpler, and for a professional, quicker to type!!

PHP can do more than adding though, the four most popular mathematical functions can be used along with main others.

See the next page for more...

<?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!!


© Varun Thirayan http://www.apdz.com
View the original article Playing with Numbers