(click anywhere to close)

# [PHP] Numbers & Operators

category: Website | course: PHP | difficulty:

Now that we know how to create lots of different variables of lots of different types, it’s time to actually start doing something with them. Computers are, at their core, number crunching machines, performing complex math the whole time. Math, however simple or complex, will always play a big part in your programs as well, which is we need an easy and quick way to perform those math operations. Not surprisingly, for this we can use operators.

Some other data types have very important operators as well, which I will discuss when their chapter arrives. Also, the syntax used for type casting also used casting operators.

## Arithmetic Operators

The four basic arithmetic operators you’ve known all your life are available, plus a few more.

 Operator Description `+` Addition `-` Subtraction `*` Multiplication `/` Division `%` Modulus* `**` Exponentiation
```\$a = 1 + 2 - 2; //\$a is 1
\$b = 2 * 2 / 4; //\$b is 1
\$c = 2**4       //\$c is 2^4 = 2*2*2*2 = 16
```

## Incrementing/Decrementing

Adding exactly one to a number, or subtracting one from it, is such a common operation that it has its own operators. Be sure to notice, however, that placing this operator before or after the variable makes a huge difference.

 Operator Description `\$x++` Adds 1 to \$x, then returns \$x. `\$x--` Subtracts 1 from \$x, then returns \$x `++\$x` Returns \$x, then adds 1 to \$x `--\$x` Returns \$x, then subtracts 1 from \$x
```\$a = 0;
\$b = \$a++; //\$b is 1, \$a is 1
\$b = ++\$a; //\$b is 1, \$a is 2
```

## Assignment Operators

Not so long ago I told you: “computers can’t have loose data flying around”. Similar to how we store temporary results in our head when performing mental arithmetic, computers always need to store the results of a certain computation somewhere. In real life, people could say things like 1 + 1 = 2 or 1 * 2 * 3 = 6. Not in the computer world.

Every statement where you assign a value, you must have a variable on the left hand side, and all the values and operators on the right hand side. So, every time you want to update a variable, you simply assign its old value plus some additional computations.

```\$a = 5;
\$a = \$a + 5; //\$a is 10
\$a + 5; //ERROR.
```

For those that don’t know: we’ve seen in the beginning that we can use the assignment operator ( `=` ) to assign a value to a variable; everything on the left of that operator is called the left hand side, and everything on the right the right hand side. Also, every value or variable around an operator is called an operand.

This seems tedious and verbose, so you’ll be happy to hear there’s a much shorter and easier way to write these assignments.

 Operator Shorthand Full `+=` `x += y` `x = x + y` `-=` `x -= y` `x = x - y` `*=` `x *= y` `x = x * y` `/=` `x /= y` `x = x / y` `%=` `x %= y` `x = x % y` `**=` `x **= y` `x = x**y`

I think this is a very important concept to understand completely, as you’ll need it very often, and it works the exact same way in all programming languages. You just need to remind yourself that you can’t have loose data – everything needs to be saved into a variable. (Or put into a function.)

Knowing this, we can see that the `++` and `--` operators are just shortcuts for `+= 1` and `-= 1`.

```\$a = 10;
\$a += 5; //\$a is 15
\$a *= 2; //\$a is 30
```

## Not a Number

What would happen if we were to (accidentally) use these operators with other data types than floats or integers? In most cases, it would just throw an error. There are three special cases though:

• Booleans: the value `true` resolves to the integer 1, while `false` resolves to 0.
• Strings: When converting strings to integers, PHP expects the number to be at the start of the string. It tries to make the number as large as possible. So, if a string doesn’t contain a number at the start, it will always resolve to `0`.
• Special Floats: There are some floating point numbers that can be written down or calculated with PHP, but can’t be written exactly as a number. (Sounds weird, I know.) We call these not a number.

Among those special floats are infinity ( `INF` ) and values that can’t be calculated (such as `sqrt(-1.0)`, the square root of minus one). To check if a float is special, and therefore not a number we can perform calculations with, we use

`is_nan(\$float);`

To merely check whether a value is infinite or not, which also means we can't really do anything with it, use

`is_infinite(\$float);`
```echo "7 dwarves" + "1 snow white";   //Prints 8
echo "Dwarves, 7" + "Snow white, 1"; //Prints 0

\$a = is_nan(true);       //\$a is false
\$a = is_nan(acos(1.1));  //\$a is true
```

## Operator Precedence

The precedence of operators is just like you’re used to.

• The `**` operator is evaluated first
• The `*`, `/` and `%` are evaluated second
• The `+` and `–` are evaluated third

If you want to change this order, by grouping certain terms together, you can use the familiar parentheses ( `( )` ).

```echo 5 + 5 * 3;   //Prints 5 + 15 = 20
echo (5 + 5) * 3; //Prints 10 * 3 = 30
```

## *Modulus

The modulus of x and y is simply the remainder after dividing x by y. For example:

16 % 6 = 4            (because 16 / 6 = 2 with remainder 4)
18 % 3 = 0            (because 18 = 3 * 6, so there’s no remainder)
4 % 3 = 1            (because 4 / 3 = 1 with remainder 1)

CONTINUE WITH THIS COURSE
Do you like my tutorials?
To keep this site running, donate some motivational food!
Crisps
(€2.00)
Chocolate Milk
(€3.50)
Pizza
(€5.00)