Arithmetic Operators - PHP - P16

Опубликовано: 15 Март 2026
на канале: Dino Cajic
176
6

Arithmetic deals with the study of numbers, especially with operations on those numbers, such as addition, subtraction, multiplication, and division. In mathematics, those operations are called arithmetic operations. Arithmetic operators are then the operators that are used in arithmetic operations.

In PHP, we have a few arithmetic operators. They include: addition, subtraction, multiplication, division, modulus, and exponential operators.

Let's just run through the basic ones first. I mean, after all, it is just basic math.

$a = 2 + 2; // $a = 4
$b = 5 - 3; // $b = 2
$c = $a * $b; // $b = 8
$d = $c / 2; // $d = 4

Let's take a look at the modulus operator in more detail. It confuses people, but it's literally just the remainder. If you do elementary division, and you say 3/2, that equals 1 with a remainder of 1. Two goes into three 1 time, and there's one left over. That remainder of 1 is what the modulus operator produces.

The modulus operator is represented with the percentage symbol %.

$e = $d % 2;
// $e = 4 % 2
// $e = 0 since there's no remainder
$f = 5 % 2;
// $f = 1 since there's a remainder of 1

The exponential operator just raises the number to the power provided. So if you said 2³, that just equals 2 * 2 * 2, which is 8. The exponential operator in PHP is expressed using the double asterisk symbol (**).

$g = 2 ** 3;
// $g = 2^3
// $g = 8

You're not limited to using one arithmetic operator per expression. The operations follow the same precedence as in math. For example, the multiplication operator is executed before the addition operator. 

Let's see how PHP would evaluate this expression.

$h = $a + $b * $c;
// $h = $a + ($b * $c)
// $h = $a + (2 * $c)
// Remember, $c = $a * $b
// $h = $a + (2 * ($a * $b))
// $h = $a + (2 * (4 * 2))
// $h = $a + (2 * 8)
// $h = $a + 16
// $h = 4 + 16
// $h = 20

Once we finish going through all of the operators in PHP, we're going to look at operator precedence in detail in a later video.

Read the full article on my website
https://www.dinocajic.com/php-arithme...

Code for this tutorial
https://github.com/dinocajic/php-7-yo...

Full Code
https://github.com/dinocajic/php-7-yo...

PHP Playlist
   • PHP Tutorial  

--
Dino Cajic
Author and Head of IT

Homepage: https://www.dinocajic.com
GitHub: https://github.com/dinocajic
Medium:   / dinocajic  
Instagram:   / think.dino  
LinkedIn:   / dinocajic  
Twitter:   / dino_cajic  

My Books
An Illustrative Introduction to Algorithms
https://www.amazon.com/dp/1686863268

Laravel Excel: Using Laravel to Import Data
https://amzn.to/4925ylw

Code Along With Me - PHP: From Basic to Advanced PHP Techniques
https://amzn.to/3M6tlGN