Operators
An expression in PHP is a value built from values and operators. This page takes the arithmetic ones, the two ways to compare, the logical ones and the order in which they run.
Arithmetic
Four signs handle school math, plus two extras: percent keeps the remainder and double star raises to a power:
<?php echo 7 + 3, PHP_EOL; echo 7 - 3, PHP_EOL; echo 7 * 3, PHP_EOL; echo 7 / 2, PHP_EOL; echo 7 % 3, PHP_EOL; echo 2 ** 3, PHP_EOL;
php arithmetic.php
10
4
21
3.5
1
8
Division gives a float as soon as the result is not a whole number: 7 / 2 is 3.5, never 3. The percent sign keeps only what is left over, and 7 % 3 leaves 1.
Compare strictly
Loose comparison juggles types first, which hides bugs. Strict comparison checks the type as well, so prefer it everywhere:
<?php var_dump(1 == true); var_dump(1 === true); var_dump("42" === 42);
php comparison.php
bool(true)
bool(false)
bool(false)
The loose check turns 1 into true and says yes; the strict check compares an integer with a boolean and says no. Two equals signs are enough only when both sides are already the same type.
Logical operators
Double ampersand means and, double pipe means or, and the exclamation mark flips a value. Combine them to build real conditions:
<?php $adult = true; $paid = false; var_dump($adult && $paid); var_dump($adult || $paid); var_dump(!$paid);
php logical.php
bool(false)
bool(true)
bool(true)
The first check asks for both conditions and gets only one, so it fails; the second asks for either one and passes; the third flips false into true.
Precedence and parentheses
Multiplication runs before addition, and and runs before or. Parentheses beat every rule, so add them whenever a line needs a second look:
<?php echo 2 + 3 * 4, PHP_EOL; echo (2 + 3) * 4, PHP_EOL; $ok = true || false && false; var_dump($ok); $ok = true and false; var_dump($ok); $ok = true && false; var_dump($ok);
php precedence.php
14
20
bool(true)
bool(true)
bool(false)
In the third line && runs first, so the check is true || false, which is true. The word and sits lower than the assignment, so $ok = true and false stores true and throws the rest away; the symbol && sits higher, and the last line stores false.
Exercises
- Print the result of 10 / 4 and of 10 % 4 on two lines. Which one is a float, and why?
- Print 2 ** 10 and 2 ** 0. What does a power of zero always give?
- Check "5" against 5 with both == and ===, and explain the two lines.
- Build a condition that is true only when a person is an adult and has paid, then flip one value and watch the result change.
Questions
A new set of questions in a new order every time. Choose one answer for each, then check.
-
Which operator keeps only the remainder of a division?
Answer
%The percent sign is the modulo operator: 7 % 3 gives 1, the remainder of the division.
-
What does
===check that==does not?Answer
The type of both values as well as their value
The strict comparison is true only when the values and their types match, so 1 === true is false.
-
Which operator means "either one, or both"?
Answer
||The double pipe is the logical or: it is true when at least one side is true.
-
What does an expression in parentheses do?
Answer
It runs first, whatever the precedence table says
Parentheses group an expression and give it the highest precedence, so (2 + 3) * 4 is 20 and not 14.
-
What does this program print?
<?php echo 9 / 2;Answer
4.5
The division of two integers gives a float when the result is not whole, so PHP prints 4.5.
-
What does this program print?
<?php echo 9 % 2;Answer
1
The modulo keeps the remainder: 9 is 4 times 2 plus 1, so the result is 1.
-
What does this program print?
<?php echo 2 ** 10;Answer
1024
The double star raises to a power: 2 to the tenth is 1024.
-
What does this program print?
<?php var_dump("7" === 7);Answer
bool(false)
A string and an integer never match under the strict comparison, whatever they look like.
-
What does this program print?
<?php echo 2 + 3 * 4;Answer
14
The multiplication runs before the addition, so the expression is 2 + 12 and prints 14.
-
What does this program print?
<?php $ok = true and false; var_dump($ok);Answer
bool(true)
The word and has a lower precedence than the assignment, so true is stored first and the rest of the line is evaluated after it.
The code on this page was run with PHP 8.5.13 (php -v) in Alpine Linux, with the commands shown above.
Article author: Arthur Isaev