Lesson 4 of 100
Operators12 min5 checksPHP 8.5

Operators: Calculate and Compare

Learn how PHP arithmetic, assignment and comparison operators work, why strict equality matters, and how precedence shapes an expression.

01 · THE IDEAREAD → TRY → LEARN

What an operator does

An operator takes one or more values and produces a new value, so the whole construction is itself an expression. Operators are grouped by how many values they take. A unary operator takes one, like ! or ++. A binary operator takes two, like + or -. PHP has a single ternary operator, ? :, which takes three.

In this lesson we focus on three families you will use constantly: arithmetic, assignment and comparison.

Arithmetic operators

The familiar symbols work as you expect: + adds, - subtracts, * multiplies and / divides. PHP adds % for the remainder after integer division and ** for exponentiation. The function intdiv() gives whole-number division when you do not want a fractional result.

Note that / returns a float when the division is not exact, while % works on integers and keeps the sign of the left operand.

Example
<?php
$a = 17;
$b = 5;
echo json_encode([$a + $b, $a - $b, $a * $b, $a / $b, $a % $b, $a ** 2]);
Output
[22,12,85,3.4,2,289]

The result is a list of the sum, difference, product, quotient, remainder and square of $a.

Comparison: loose versus strict

PHP offers two kinds of equality. The loose operators == and != may convert types before comparing, so the integer 5 and the string "5" are considered equal. The strict operators === and !== require the same value and the same type.

For most application code, prefer strict comparison. It removes surprises that come from silent conversion and makes your intent obvious to the reader.

The spaceship operator <=> returns -1, 0 or 1 depending on whether the left side is smaller than, equal to, or greater than the right side. It is handy for sorting callbacks.

Example
<?php
$n = 5;
$s = "5";
echo json_encode([$n == $s, $n === $s, $n !== $s, $n <=> 7, 7 <=> $n]);
Output
[true,false,true,-1,1]

Here the loose check is true, the strict check is false, and the two spaceship results are -1 and 1.

Assignment and precedence

The plain = stores a value in a variable. Combined assignment operators such as += and *= update a variable using its current value, which saves you from repeating its name.

When an expression mixes operators, precedence decides which runs first. Multiplication binds tighter than addition, so 2 + 3 * 4 is 14. Parentheses override that order. When in doubt, add parentheses: they cost nothing and help the next reader.

Example
<?php
$total = 2 + 3 * 4;
$grouped = (2 + 3) * 4;
$count = 10;
$count += 5;
$count *= 2;
echo json_encode(["total" => $total, "grouped" => $grouped, "count" => $count]);
Output
{"total":14,"grouped":20,"count":30}

After the two combined assignments, $count goes from 10 to 15 and then to 30.

Worked reasoning

Suppose you want to compare 7 with 3. The sum is 10, the product is 21, the remainder is 1 (since 7 = 2 * 3 + 1) and the values are not strictly equal. Now take -7 and 3: the remainder is -1, because the sign follows the left operand. Finally, take 5 and "5". Arithmetic treats the numeric string like a number, so the sum is 10, but === reports false because an integer and a string differ in type.

Common mistakes

  • Using == when you meant ===, which hides type differences.
  • Forgetting that / can return a float.
  • Taking the remainder with % when the right operand is zero, which throws an error.
  • Relying on precedence in long expressions instead of adding parentheses.

Exercise contract

Write a function analyzePair($a, $b) that returns a list of four values, in this order:

  1. the sum $a + $b
  2. the product $a * $b
  3. the remainder $a % $b
  4. a boolean that is true only when $a and $b are strictly identical

The tests never pass 0 as $b. One test passes the integer 5 and the string "5", so you must use strict comparison to get it right.

Recap

Arithmetic operators calculate, assignment operators store and update, and comparison operators produce booleans. Prefer === over ==, and use parentheses to make evaluation order clear.

Before you start

Variables · Types

You’ll practice
  • Apply arithmetic operators
  • Compare values with strict equality
Your task

Make analyzePair() pass all 5 checks. Run your code as often as you like, then check your answer.

exercise.phpSaved in this browserPHP 8.5
CONSOLE & CHECKS● Ready

Ready when you are

Your output will appear here.
Make it work. Then make it pass.

PHP loads on your first run. Each check starts fresh. Your code stays in this browser; clearing site data removes your progress. A check or the task looks wrong? Report a problem with this exercise.