Playing with Numbers

by Varun Thirayan
APDZ.com

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...


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