Math: Operators, Properties, and Methods
Table of Contents
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');
?>
HTMLI 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');
?>
HTMLI 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');
?>
HTMLI have 6 cats
Subtraction
PHP<?php
// subtracting numbers
$candy = 12;
$eaten = 3;
$left = $candy - $eaten;
echo('I have ' . $left . ' candies left');
?>
HTMLI have 9 candies left
Here's the shorthand way to subtract 1
:
PHP<?php
$candy = 12;
$candy--; // subtracts 1
echo('I have ' . $candy . ' candies');
?>
HTMLI 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');
?>
HTMLI have 5 candies
Multiplication
PHP<?php
// multiplying numbers
$tacos = 2;
$guests = 20;
$needed = $guests * $tacos;
echo('I need ' . $needed . ' tacos');
?>
HTMLI 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');
?>
HTMLI need 40 tacos
Division
PHP<?php
// dividing numbers
$burgers = 12;
$guests = 6;
$each = $burgers / $guests;
echo('Each guest can eat ' . $each . ' burgers');
?>
HTMLEach 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');
?>
HTMLEach guest can eat 2 burgers
Modulus
PHP<?php
// modulus operator
$bottles = 14;
$people = 4;
$extra = $bottles % $people;
echo('There are ' . $extra . ' extra bottles');
?>
HTMLThere 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');
?>
HTMLThere 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;
?>
HTMLx 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);
?>
HTML2.718281828459
Pi
The much known and beloved pi is provided via the M_PI
constant.
PHP<?php
echo(M_PI);
?>
HTML3.1415926535898
Euler's Constant
Euler's constant is provided to us via the M_EULER
constant.
PHP<?php
echo(M_EULER);
?>
HTML0.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));
?>
HTML3 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));
?>
HTML3
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));
?>
HTML4
Logarithm
Get the natural logarithm of a number by using the log()
function:
PHP<?php
$number = 3.1415;
echo(log($number));
?>
HTML1.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));
?>
HTML74
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));
?>
HTML23
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));
?>
HTML125
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);
?>
HTMLI 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));
?>
HTML13 83
Square Root
Calculate the square root of a number by using the sqrt()
function:
PHP<?php
echo(sqrt(1337));
?>
HTML36.565010597564
That's the end of it for Math in PHP! ๐ค๐ค๐ค
- Getting Started with Solid
- Getting Started with Express
- Create an RSS Reader in Node
- How to Set Up Cron Jobs in Linux
- How to deploy a .NET app using Docker
- How to build a Discord bot using TypeScript
- How to deploy a Node app using Docker
- How to Scrape the Web using Node.js and Puppeteer
- Getting Started with Handlebars.js
- Getting User Location using JavaScript's Geolocation API
- Learn how to build a Slack Bot using Node.js
- Getting Started with React