**Balfour**BSc

Jamie **Balfour** BSc

Full stack developer

Mathematics is an important part of programming. In fact, programming is a mathematical concept. This article will discuss how mathematics in PHP works.

There are a collection of mathematical operators that perform different tasks. The expressions used to represent such operators are shown in the following table, along with an example of each.

Name | Description | Operator Code | Example | Result |
---|---|---|---|---|

Addition | Simply used to add two numbers together. | `+` |
`5 + 10` |
15 |

Subtract | Used to subtract the second number from the first | `-` |
`10 - 5` |
5 |

Multiply | Used to multiply two numbers | `*` |
`7 * 4` |
28 |

Divide | Used to divide the first number by the second number and return a floating point number | `/` |
`50 / 7` |
7.14... |

Raise | Raise a number to another number in the same way as
the mathematical representation x^{y} |
`**` |
`2**7` |
128 |

Modulus | Used to get the remainder of a division as an integer. | `%` |
`5 % 4` |
1 |

These operations can be used to return a result as an integer or floating point number.

A boolean operation can be used to return a boolean value (`true`

or `false`

).
The table below specifies all of these available in PHP:

Name | Description | Operator Code | Example (of truth) |
---|---|---|---|

Greater than | True if the first expression is greater than the second expression | > | 7 > 4 |

Less than | True if the first expression is less than the second expression | < | 7 < 16 |

Greater than or equal to | True if the first expression is greater than or equal to the second expression | >= | 17 >= 16 |

Less than or equal to | True if the first expression is less than or equal to the second expression | <= | 16 <= 16 |

Equal to | True if both expressions are equal | == | 16 == 16 |

Not equal | True if both expressions are not equal | != | 16 != 7 |

The results of these operations can be placed in a boolean variable as shown in the following sample:

PHP

$a = 5; $b = 9; $c = $a + $b - 7; $result = $c > $a; echo $result;

The above sample should return the boolean `true`

.

Boolean logic also uses other operators. These can also be represented in PHP and are also known as bitwise operators. Formal logic and specification defines the way these work and PHP contains expressions that form statements like the following which can be constructed in PHP:

x = (a ∧ b) ⊕ (c ∨ d)

The above expression will evaluate true when *a* and *b* are both false and either *c* or *d *are
true or when *a* and *b* are both true and both *c* and *d* are false due to the ⊕ operator.

The following table shows these operators.

Name | Description | Logical symbol | Operator code | Example (of truth) |
---|---|---|---|---|

And | Logical conjunction, true when both expressions are true. |
∧ | `&&` |
`true && true` |

Or | Logical disjunction, true when either expressions are true. |
∨ | `||` |
`true || false` |

Not | Logical negation, returns true when given a false expression. |
¬ | `!` |
`!(false)` |

The following is a truth table that represents each of the operators shown above for some P and some Q.

P | Q | ¬P | P∧Q | P∨Q |
---|---|---|---|---|

T | T | F | T | T |

T | F | F | F | T |

F | T | T | F | T |

F | F | T | F | F |

Most languages have a shorthand method for increasing a value or variable by another value that saves writing the whole assignment and mathematical expression.

For instance, consider the following:

PHP

$x = 10; echo $x; $x = $x + 1; echo $x; $x = $x - 1; echo $x;

The method of increasing the value of `$x`

is
to reassign the value with the value plus one. What if there
were a simpler, more syntactically concise method of doing
this?

Meet incrementing and decrementing values. In reference to a value or a variable, incrementing and decrementing are very important as in many programming languages they are able to produce small performance benefits.

PHP increments with `$x += 1`

and decrements with `$x -= 1`

, but it
also offers `$x++`

and
`$x--`

as well as the
`++$x`

and `--$x`

syntaxes:

PHP

$x = 10; echo $x; $x += 1; echo $x; $x -= 1; echo $x; $x--; echo $x; $x++; echo $x; --$x; echo $x; ++$x; echo $x;

PHP also offers two types of increment and decrement: pre and post:

`$a++` |
Post incrementing |

` ++$a` |
Pre incrementing |

In both cases, these add 1 to the variable `$a`

. In the first case
(post incrementing), it is done after the call to the variable.
In the second case (pre incrementing) it is done before the call
to the variable. This next example shows this:

PHP

$x = 10; //Will display 10 echo $x++; //The value of $x is now 11 //When the next pre-increment is applied it will display 12 echo ++$x;

Welcome!

Welcome to my website. My name is Jamie Balfour and I am
a computer scientist and a teacher. My website focuses on me but I also write
articles and reviews on and related to technology as well as
tutorials designed to help you learn things I know.

Site news

ZPE 1.6.7 is now available to download. It's free too!

@jamiebalfour04

I was at first a bit worried that #PokemonLetsGoEevee was going to be a let down but still went ahead and bought it… https://t.co/60LVeA6AOZ 3 days ago |

Retweet @CarlosMatos80: Bitconnect Wasnt Just A Coin, It Was A Revolution Against The Establishment And Criminals
With A Card This Big It Is Im…11 days ago |

Wow! I can’t put it in words but my favourite game of all time, #warcraft3 is being remastered as #WarcraftReforged… https://t.co/63dEem2WSn 18 days ago |

I was right. In fact, it's pretty much confirmed in the keynote - they use SODIMMs. 19 days ago |

There’s only one CPU with the specs of the top #MacMini2018 CPU which suggests to me that it uses an Intel Core i7… https://t.co/6EM7rvTsOg 19 days ago |

Get in touch