Math: Operators, Properties, and Methods

Updated onbyAlan Morel
Math: Operators, Properties, and Methods

PHP 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 multiple 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; ('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! ๐Ÿค“๐Ÿค“๐Ÿค“

Next Lesson ยป
Copyright ยฉ 2017 - 2024 Sabe.io. All rights reserved. Made with โค in NY.