PHP Arithmetic Operators

What’s Arithmetic?

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.

 

<?php$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 %.

 

<?php$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 (**).

 

<?php$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.

 

<?php$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?>

 

That’s it for PHP Arithmetic Operators. Once we finish going through all of the operators in PHP, we’re going to look at operator precedence in detail in a later article.

 

 

PHP Tutorial Series

Continue your learning with these articles

PHP Assignment Operator

What do you mean assignment? I thought I was out of school.

PHP – P15: Assignment Operator

The PHP assignment operator (=) assigns the value, or expression, to the variable; and yes, it’s different from the equality comparison operator (==).

PHP Arithmetic Operators

What’s Arithmetic?

PHP – P16: Arithmetic Operators

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.

PHP Comparison Operators

SO YOU WANT TO COMPARE STUFF?

PHP – P17: COMPARISON OPERATORS

PHP Comparison Operators, or relational operators, test some type of relation between two values. Greater-than, less-than, is equal to. These are just a few.

Leave a Reply