TExcelTAD
Home › PHP › PHP Operators

PHP Operators

Operators are the symbols that combine, compare and change values: + adds numbers, == compares them, . glues strings together and ?? picks a fallback. PHP has a few operators you will not find in every language, so this lesson walks through all the groups you need day to day.

Arithmetic operators

Operator Name Example Result
+ Addition 7 + 2 9
- Subtraction 7 - 2 5
* Multiplication 7 * 2 14
/ Division 7 / 2 3.5
% Modulo (remainder) 7 % 2 1
** Exponentiation 7 ** 2 49

Note that / returns a float when the division is not exact. Use intdiv() when you want whole-number division.

php · try it yourself
<?php
$apples = 17;
$baskets = 5;

echo "Total / baskets = ", $apples / $baskets, "\n";
echo "Whole apples per basket: ", intdiv($apples, $baskets), "\n";
echo "Apples left over: ", $apples % $baskets, "\n";
echo "2 to the power 10: ", 2 ** 10, "\n";
Total / baskets = 3.4
Whole apples per basket: 3
Apples left over: 2
2 to the power 10: 1024

Assignment operators

= stores a value. The combined forms update a variable using its own current value.

Operator Same as
$x += 3 $x = $x + 3
$x -= 3 $x = $x - 3
$x *= 3 $x = $x * 3
$x /= 3 $x = $x / 3
$x %= 3 $x = $x % 3
$x **= 3 $x = $x ** 3
$s .= "!" $s = $s . "!"
$x ??= 5 assign 5 only if $x is null or unset

Increment and decrement

++ and -- add or subtract one. The position matters: pre-increment (++$i) changes the value first and then returns it, post-increment ($i++) returns the old value and then changes it.

php · try it yourself
<?php
$score = 10;
$score += 5;
$score *= 2;
echo "Score: $score\n";

$i = 1;
echo "Post: ", $i++, " (now $i)\n";
echo "Pre: ", ++$i, " (now $i)\n";

$label = "Level";
$label .= " up";
echo $label, "\n";
Score: 30
Post: 1 (now 2)
Pre: 3 (now 3)
Level up

Comparison operators

Operator True when
== values are equal after type juggling
=== values are equal and of the same type
!= or <> not equal (loose)
!== not identical
<, >, <=, >= less / greater (or equal)
<=> "spaceship": returns -1, 0 or 1

The difference between == and === is one of the most important things to learn in PHP. Loose comparison converts types before comparing; strict comparison never does.

php · try it yourself
<?php
var_dump(5 == "5");     // loose: the string is converted to a number
var_dump(5 === "5");    // strict: int vs string
var_dump(0 == "");      // PHP 8: false, comparing number with non-numeric string
var_dump(null == false);
var_dump(3 <=> 7);
var_dump("b" <=> "a");
bool(true)
bool(false)
bool(false)
bool(true)
int(-1)
int(1)

Tip: Prefer === and !== unless you have a specific reason to allow type conversion. It avoids a whole class of surprising bugs.

Logical operators

Operator Meaning
&& / and both sides true
|| / or at least one side true
xor exactly one side true
! not

&& and || short-circuit: the right side is not evaluated if the left side already decides the result. The word forms and/or have lower precedence than =, which makes $x = true and false; assign true. Stick with && and ||.

String, null and conditional operators

  • . concatenates strings; .= appends.
  • ?: (ternary) picks one of two values: $cond ? $a : $b.
  • ?: without the middle part (the "Elvis" operator) returns the left side if it is truthy.
  • ?? (null coalescing) returns the left side if it exists and is not null.
php · try it yourself
<?php
$user = ["name" => "Mai", "nickname" => null, "points" => 0];

$age = 20;
echo "Status: " . ($age >= 18 ? "adult" : "minor") . "\n";

echo "Nickname: " . ($user["nickname"] ?? "none") . "\n";
echo "City: " . ($user["city"] ?? "unknown") . "\n";

// ?: treats 0 as falsy, ?? only cares about null
echo "Points (?:): " . ($user["points"] ?: "no points") . "\n";
echo "Points (??): " . ($user["points"] ?? "no points") . "\n";

$isMember = true;
$hasCoupon = false;
var_dump($isMember && $hasCoupon, $isMember || $hasCoupon, $isMember xor $hasCoupon);
Status: adult
Nickname: none
City: unknown
Points (?:): no points
Points (??): 0
bool(false)
bool(true)
bool(true)

Array operators

+ on arrays creates a union: keys from the left array win, and only missing keys are added from the right. == checks for the same key/value pairs, === also requires the same order and types.

Operator precedence

When an expression mixes operators, higher-precedence ones run first: 2 + 3 * 4 is 14, not 20. Roughly from high to low: **, ++ --, !, * / %, + -, ., comparisons, &&, ||, ??, ? :, assignment, then and/xor/or. When in doubt, add parentheses — they cost nothing and make intent obvious.

Test yourself

What does var_dump("10" === 10); print?

  1. bool(true)
  2. bool(false)
  3. int(10)
  4. An error

After $x = null; $y = $x ?? 'guest';, what is $y?

  1. null
  2. ''
  3. 'guest'
  4. false

Which operator joins two strings in PHP?

  1. +
  2. &
  3. .
  4. ~