# Math: Operators, Properties, and Methods

Updated on Dec 22, 2021 by [Alan Morel](/content/alanmorel/index.html)

## Table of Contents

1. [Arithmetic Operators](/content/classes/php/math#arithmetic-operators/index.html)
2. [Math Properties](/content/classes/php/math#math-properties/index.html)
3. [Math Functions](/content/classes/php/math#math-functions/index.html)

[PHP](/content/classes/php/index.html) makes working with math very easy. Not only does it offer the **arithmetic operators** you would expect from a programming language, but it also offers mathematical properties and functions so we do not have to implement them ourselves. This lets you focus on writing the logic of your program and not reinventing the wheel.

## Arithmetic Operators

**Arithmetic operators** are what let you manipulate numbers in PHP. Here are ones it offers.

### Addition

```php
<?php
    // adding numbers
    $cats = 4;
    $dogs = 3;

$pets = $cats + $dogs;
    echo('I have ' . $pets . ' pets');
?>
```

```html
I have 7 pets
```

Here is a shorthand way to add a `1` to the value of a variable:

```php
<?php
    $cats = 4;
    $cats++; // adds 1

echo('I have ' . $cats . ' cats');
?>
```

```html
I have 5 cats
```

Here is the shorthand way to add an arbitrary number:

```php
<?php
    $cats = 4;
    $cats += 2; // adds 2 cats

echo('I have ' . $cats . ' cats');
?>
```

```html
I have 6 cats
```

### Subtraction

```php
<?php
    // subtracting numbers
    $candy = 12;
    $eaten = 3;

$left = $candy - $eaten;

echo('I have ' . $left . ' candies left');
?>
```

```html
I have 9 candies left
```

Here's the shorthand way to subtract `1`:

```php
<?php
    $candy = 12;
    $candy--; // subtracts 1

echo('I have ' . $candy . ' candies');
?>
```

```html
I have 11 candies
```

Here's the shorthand way to subtract an arbitrary number:

```php
<?php
    $candy = 12;
    $candy -= 7; // subtracts 7 candies

echo('I have ' . $candy . ' candies');
?>
```

```html
I have 5 candies
```

### Multiplication

```php
<?php
    // multiplying numbers
    $tacos = 2;
    $guests = 20;
    $needed = $guests * $tacos;

echo('I need ' . $needed . ' tacos');
?>
```

```html
I need 40 tacos
```

Here is the shorthand way to multiply a variable:

```php
<?php
    $tacos = 2;
    $tacos *= 20; // multiplies the value by 20

echo('I need ' . $tacos . ' tacos');
?>
```

```html
I need 40 tacos
```

### Division

```php
<?php
    // dividing numbers
    $burgers = 12;
    $guests = 6;
    $each = $burgers / $guests;

echo('Each guest can eat ' . $each . ' burgers');
?>
```

```html
Each guest can eat 2 burgers
```

Here's the shorthand way to divide:

```php
<?php
    $burgers = 12;
    $burgers /= 6; // divides the value by 6

echo('Each guest can eat ' . $burgers . ' burgers');
?>
```

```html
Each guest can eat 2 burgers
```

### Modulus

```php
<?php
    // modulus operator
    $bottles = 14;
    $people = 4;
    $extra = $bottles % $people;

echo('There are ' . $extra . ' extra bottles');
?>
```

```html
There are 2 extra bottles
```

Here is the shorthand way to perform modulus:

```php
<?php
    $bottles = 14;
    $bottles %= 4; // does modulus 4

echo('There are ' . $bottles . ' extra bottles');
?>
```

```html
There are 2 extra bottles
```

### Order of Operations

**Order of Operations**, sometimes known as **PEMDAS**, is respected in PHP. Here's an example of it in action:

```php
<?php
    $x = (5 * 3) + 10 / 2;

echo('x is ' . $x);
?>
```

```html
x is 20
```

## Math Properties

To avoid making people define it themselves, PHP provides many useful mathematical constants that we can use whenever applicable.

### Euler's Number

**Euler's number** is provided to us via the `M_E` constant.

```php
<?php
    echo(M_E);
?>
```

```html
2.718281828459
```

### Pi

The much known and beloved **pi** is provided via the `M_PI` constant.

```php
<?php
    echo(M_PI);
?>
```

```html
3.1415926535898
```

### Euler's Constant

**Euler's constant** is provided to us via the `M_EULER` constant.

```php
<?php
    echo(M_EULER);
?>
```

```html
0.57721566490153
```

## Math Functions

In addition to mathematical properties, PHP also implements some commonly used mathematical functions that we can use whenever we want.

### Absolute Value

Get the **absolute value** of a number by passing in the number to `abs()` function:

```php
<?php
    $number1 = 3;
    $number2 = -6;

echo(abs($number1) . ' ' . abs($number2));
?>
```

```html
3 6
```

### Floor

Round any number down to the nearest whole number by calling the `floor()` function on it:

```php
<?php
    $number = 3.1415;

echo(floor($number));
?>
```

```html
3
```

### Ceiling

Round any number up to the nearest whole number by calling the `ceil()` function on it:

```php
<?php
    $number = 3.1415;

echo(ceil($number));
?>
```

```html
4
```

### Logarithm

Get the **natural logarithm** of a number by using the `log()` function:

```php
<?php
    $number = 3.1415;

echo(log($number));
?>
```

```html
1.1447003928609
```

### Maximum

Get the highest number from a set of numbers by using the `max()` function:

```php
<?php
    echo(max(35, 23, 52, 34, 74, 43));
?>
```

```html
74
```

### Minimum

Get the lowest number from a set of numbers by using the `min()` function:

```php
<?php
    echo(min(35, 23, 52, 34, 74, 43));
?>
```

```html
23
```

### Power

Calculate the power of a number by using the `pow()` function. The first parameter is the base and the second parameter is the exponent.

```php
<?php
    echo(pow(5, 3));
?>
```

```html
125
```

### Random

Return a random number between a range that you define by using the `rand()` function:

```php
<?php
    $roll = rand(1, 6);
    echo('I just rolled a ' . $roll);
?>
```

```html
I just rolled a 6
```

### Round

Round a number to the nearest integer by using the `round()` function on it:

```php
<?php
    $number1 = 13.37;
    $number2 = 82.65;

echo(round($number1) . ' ' . round($number2));
?>
```

```html
13 83
```

### Square Root

Calculate the square root of a number by using the `sqrt()` function:

```php
<?php
    echo(sqrt(1337));
?>
```

```html
36.565010597564
```

That's the end of it for Math in PHP! 🤓🤓🤓
