# math operators in PHP

Operator |
Description |

+ | The addition operator |

- | The subtraction operator , can also be used for negation like this -9 |

* | The multiplication operator |

/ | the division operator |

% | the modulus operator , returns the remainder after division . For example 25 % 3 would give us 1 . |

Here is an example

<?php

$number1 = 9;

$number2 = 4;

echo $number1 + $number2;

?>

Which produces the following

13

**Incrementing **

To increment a variable in PHP we do the following

$yearsindebt++;

This increments the $yearsindebt variable by one , we can also do the following .

$mybill += 20;

which is the same as saying $mybill = $mybill + 20;

**Order Of Precedence **

The math operators all have to obey an order of precedence which is used in mathematics . This often catches out beginners . Lets look at an example

$calc = 25 + 4 * 2;

What do you think the value of $calc is , well you could add 25 + 4 and get 29 then multiply by 2 and get 58 . Wrong . The rules of precedence say that multiplication gets calculated before addition so you get 4 * 2 is 8 and then 25 + 8 = 33 . 33 is the correct answer . A big difference from 58 .

At school I was taught to use BODMAS to help remember order of precedence this is basically Brackets , Division , Multiplication , Addition , Subtraction. What if you want to add the 25 + 4 first and then multiply this by 2 , well you can do the following.

$calc = (25 + 4) * 2;

Now the brackets get evaluated first because they come before multiplication in the order of precedence..