Last time we looked at text and strings in variables, in
this episode we're going to continue with our exploration of
PHP variables and delve deeper into math and number handling
in PHP.

Using numbers is not much different to using text and
strings, you allocate variables and fill them in, using
exactly the same techniques as you do using strings & text.

Basic Operators

The standard arithmetic operators are available in PHP and
these are the same as in any other language:

- + Addition of numbers (20+10)
- - Subtraction of numbers (20-10)
- * Multiplication of numbers (20*10)
- / Division of numbers (20/10)

When setting up or assigning variables, you can set the
initial value to the result of a mathematical equation, as
in the following example:

$result = 2+2; // $result will be equal to 4

For subtraction and addition there's also a very handy
shortcut or two which are great for loop handling, these
shortcuts are used by appending either a double + or a
double - after the variable name:

- $a = 1; // $a equals to 1
- $a++; // $a equals 2
- $a--; // $a once again equals 1

The second operator can also be specified as '=num' :

- $a = 1; // $a equals 1
- $a+=2; // $a equals 3
- $a-=2; // $a back to 1

Why would you want to use the shorthand form? Well it turns
out that using this method is great for loops and repetitive
commands (we'll cover this more in a later article) such as
the for loop:

for($count=0;$count<10;$count++) { // Repeat what ever goes here 10 times }

In that small example, we start at 0, and keep adding one to
$count as long as we are still less than 10.

An IMPORTANT note here, is that a lot of counting structures
in PHP start at 0, so in the previous example we are
counting from 0 to 9, which is 10 times round before it
stops. If we'd changed it to <11 in the middle part, we
would have actually performed 11 counts.

Number Shifting

PHP also has a useful operator known as a barrel shifter.
The barrel shifter or Logical shift as some people may call
it is represented by the << and >> (Chevron)
operators.

When applied to a given number, the number is shifted one
bit position in that direction. If the idea of shifting
seems a little alien then it helps to think of this in terms
of binary, and if binary is a little strange, then lets have
a little recap first.

Binary what on earth is that?

Human beings are taught at school to deal with the decimal
system, in doing this we are taught that numbers start at 0
and go towards 9, once 9 is reached we move across to the
next column and continue counting.

The columns are labelled as Hundreds, Tens and Units.
Binary numbers are not much different, except that they only
go from 0 to 1 before moving across a column.

This for a computer is an absolutely perfect state of
affairs, as deep down at the very basic level in a computer,
all there is to represent numbers are a series of on or off
electrical pulses. Unfortunately this is not so easy for
humans to understand, so in order to make it easy to
understand we label across the columns in powers of 2 as
follows.

--------------------------------------- | 128 | 64 | 32 | 16 | 8 | 4 | 2 | 1 | ---------------------------------------

As you can see above, there are 8 columns. When a number is
referred to as an 8 bit number, then it is meant that there
are 8 columns across the top, a 16 bit number would continue
on from 128 to 256, then 512, 1024 and so on multiplying by
2 each time.

If we then put a binary number under the columns, EG:

--------------------------------------- | 128 | 64 | 32 | 16 | 8 | 4 | 2 | 1 | --------------------------------------- | 0 | 0 | 1 | 0 | 0 | 1 | 0 | 1 | ---------------------------------------

You can see where the '1' values lie under the columns, if
we add the columns together from right to left, which in
this case is 1 + 4 + 32 or 37 so 00100101 = 37 in decimal.
To convert back the other way, we simply keep subtracting
the largest value we are able to from right to left while
still keeping a positive whole number, so

- 37 - 128 Not possible
- 37 - 64 Not Possible
- 37 - 32 Leaves 5
- 5 - 16 Not possible
- 5 - 8 Not possible
- 5 - 4 Leaves 1
- 1 - 2 Not possible
- 1 - 1 Leaves 0 (All done, no numbers left)

So now we've had a quick recap on binary (I'm not going into
it any deeper as this is not a lesson on using binary) why
is the barrel shifter so important?

Back to the barrel shifter

As you can see from the above examples, each column in a
binary number is a multiplication of 2. The barrel shift,
shifts binary bits in a number left and right and as a
result is able to multiply and divide by 2 extremely fast.
This is very handy if for example we are working with binary
streams (such as if we where doing heavy number crunching in
a compression system), and if we are chopping up and packing
numbers.

Again, if this is a subject that interests you then there
are many white papers and educational texts available that
cover number theory, just be aware that if your divisions
and/or multiplications are powers of two, or if you need to
isolate only a single bit in a number then using the barrel
shifter is an extremely efficient way to do it.

Number comparisons

In any computer language, the ability to check numbers that
are in a given range is a must. You saw it earlier in the
example of a for loop, and like loops in general we'll cover
decisions in more detail when we cover loops and decisions
in a later article, for now though we check ranges by using
the following operators:

- $a < $b; // Is $a less than $b
- $a > $b; // Is $a greater than $b
- $a => $b; // Is $a equal to or greater than $b
- $a =< $b; // Is $a equal to or less than $b
- $a == $b; // Is $a equal to $b

Normally you would use these in an "if" statement similar to the following:

if($a > $b) { // Do stuff if a is greater than b } else { // Do stuff if it's not }

Summary

In this episode we've looked at basic math operations in
PHP, and had a recap on binary. As with strings and text
there is a huge number of functions that operate on numbers.

There are functions to generate random numbers, or do
complex math using sines & cosines.

As always I encourage you to look at the appropriate
sections in the PHP Manual, and
experiment with what you find there. Remember half of the
fun of programming is breaking things, then learning how to
fix them.

Until next time
Shawty