10. PHP Operators

What you will learn

  • Arithmetic, assignment, comparison, and logical operators
  • The string, null coalescing, and ternary operators
  • The spaceship operator
  • Operator precedence

Arithmetic and assignment

Operator Meaning Example
+ - * / Add, subtract, multiply, divide 7 / 2 is 3.5
% Remainder 7 % 3 is 1
** Power 2 ** 3 is 8
+= -= *= /= .= Change a variable using itself $x += 5
++ -- Add or subtract one $x++

PHP code

<?php
$x = 10;
$x += 5;
$x *= 2;
$x -= 4;
$x++;

$text = "Hello";
$text .= " World";

echo $x . " | " . $text;

Output

27 | Hello World

The .= operator adds text to the end of a string.

Comparison operators

Operator Meaning
=== Equal value and type
!== Not identical
==, != Equal or not equal after conversion (avoid)
< > <= >= Less, greater, and so on
<=> Spaceship: -1, 0, or 1

PHP code

<?php
var_dump(5 === "5");
var_dump(5 !== "5");
var_dump(3 <=> 7);
var_dump(7 <=> 7);
var_dump(9 <=> 7);

What you should see (inside <pre>)

bool(false)
bool(true)
int(-1)
int(0)
int(1)

The spaceship operator <=> is handy for sorting.

Logical operators

Operator Meaning
&& AND: both must be true
|| OR: at least one is true
! NOT: flips the value

PHP code

<?php
$age = 20;
$has_ticket = false;

echo "AND: " . var_export($age >= 18 && $has_ticket, true) . "<br>";
echo "OR: " . var_export($age >= 18 || $has_ticket, true) . "<br>";
echo "NOT: " . var_export(!$has_ticket, true);

What the browser shows

AND: false
OR: true
NOT: true

PHP also has the words and and or, but they have lower precedence and cause surprises. Stick with && and ||.

Ternary and null coalescing

PHP code

<?php
$age = 16;
echo ($age >= 18 ? "adult" : "minor") . "<br>";

$user = null;
echo ($user ?? "Guest") . "<br>";

$settings = ["theme" => "dark"];
echo $settings["theme"] ?? "light";
echo " / ";
echo $settings["font"] ?? "default font";

What the browser shows

minor
Guest
dark / default font

The ?? operator returns the left side if it exists and is not null, otherwise the right side. It does not raise a warning for a missing array key, which makes it ideal for reading form data and settings.

Precedence

PHP follows the usual order: multiplication and division come before addition and subtraction. Use brackets to make your meaning clear.

PHP code

<?php
echo 2 + 3 * 4;
echo " | ";
echo (2 + 3) * 4;

Output

14 | 20

Joining text and numbers

PHP 8 treats + as arithmetic only and . as joining. Mixing them without brackets can give surprising results, because + and - bind tighter than .. Wrap calculations in brackets when you build text.

PHP code

<?php
echo "Total: " . (5 + 6);

Output

Total: 11

Common mistakes

  • Writing = instead of === in a condition.
  • Using == and being surprised by conversion.
  • Using + to join text.
  • Forgetting brackets around a calculation inside joined text.
  • Using and and or instead of && and ||.

Exercise

$input may be missing. Print its value, or Anonymous if it is not set. Test it with $input = null; and with $input = "Sam";.

Show solution
<?php
$input = null;
echo $input ?? "Anonymous";

With null it prints Anonymous. With "Sam" it prints Sam.

Knowledge check

1. Which operator joins strings?

The dot (.).

2. What does ?? do?

It gives a fallback when the left side is missing or null.

3. What does 3 <=> 7 return?

-1

Summary

  • Use === and !== for comparisons, and &&, ||, ! for logic.
  • . joins text, and ?? supplies fallbacks.
  • Use brackets to make precedence clear.
Scroll to Top