PHP 8.5 OPERATORS REFERENCE

Quick Reference Guide for Modern PHP Development
Arithmetic Operators
Op Name Example
+ Addition 5 + 3 → 8
- Subtraction 5 - 3 → 2
* Multiplication 5 * 3 → 15
/ Division 10 / 4 → 2.5
% Modulus 10 % 3 → 1
** Exponentiation 2 ** 3 → 8
Assignment Operators
Op Name Example
= Assign $a = 10
+= Add assign $a += 5 → $a = $a + 5
-= Subtract assign $a -= 3
*= Multiply assign $a *= 2
/= Divide assign $a /= 4
%= Modulus assign $a %= 3
.= Concat assign $str .= " world"
??= Null coalesce assign $a ??= "default"
Tip: Use ??= for lazy loading: $cache ??= loadData() only executes if null.
Comparison Operators
Op Name Example
== Equal (loose) 5 == "5" → true
=== Identical (strict) 5 === "5" → false
!= Not equal 5 != 3 → true
!== Not identical 5 !== "5" → true
< Less than 3 < 5 → true
> Greater than 5 > 3 → true
<= Less or equal 5 <= 5 → true
>= Greater or equal 5 >= 3 → true
<=> Spaceship 1<=>2 → -1, 2<=>2 → 0, 3<=>2 → 1
Spaceship: Returns -1, 0, or 1. Perfect for usort() callbacks.
Logical Operators
Op Name Example
&& And (high prec) $valid && $active
|| Or (high prec) $admin || $mod
! Not !$expired
and And (low prec) $a and $b
or Or (low prec) $a or $b
xor Exclusive or $a xor $b
Precedence: &&/|| bind tighter than =. and/or bind looser. Use &&/|| in conditionals.
Null Handling (PHP 7.0+, 8.0+)
Op Name Example
?? Null coalesce $name ?? "Guest"
??= Null coal. assign $cache ??= loadData()
?-> Nullsafe operator $user?->profile?->bio
?? vs ?: ?? checks null only. 0 ?? 10 → 0, but 0 ?: 10 → 10. Nullsafe: Essential for Laravel optional relationships.
Ternary Operators
Op Name Example
? : Ternary $age >= 18 ? "adult" : "minor"
?: Elvis/Short $name ?: "Anonymous"
Array Operators
Op Name Example
+ Union ['a'=>1] + ['a'=>2,'b'=>3] → ['a'=>1,'b'=>3]
== Equality [1,2] == [2,1] → false
=== Identity [1,2] === ["1","2"] → false
!= Inequality [1] != [2] → true
!== Non-identity [1,2] !== [1,2] → false
Union: Left keys win. Different from array_merge() which renumbers numeric keys.
Increment/Decrement
Op Name Example
++$a Pre-increment $a=5; echo ++$a; // 6
$a++ Post-increment $a=5; echo $a++; // 5
--$a Pre-decrement $a=5; echo --$a; // 4
$a-- Post-decrement $a=5; echo $a--; // 5
String Operators
Op Name Example
. Concatenation "Hello" . " World"
.= Concat assign $log .= "\n" . $msg
Bitwise Operators
Op Name Example
& And 12 & 10 → 8
| Or 12 | 10 → 14
^ Xor 12 ^ 10 → 6
~ Not ~5 → -6
<< Shift left 5 << 2 → 20
>> Shift right 20 >> 2 → 5
Use case: Permission flags, bitmasks. if ($perms & READ_FLAG)
Type & Object Operators
Op Name Example
instanceof Instance check $user instanceof User
-> Object operator $user->name
?-> Nullsafe $user?->address?->city
:: Scope resolution User::find(1)
Spread Operator (PHP 7.4+, 8.1+)
Op Name Example
... Argument unpack max(...$array)
... Array unpack [...$arr1, ...$arr2]
... Variadic params function foo(...$args)
PHP 8.1+: Spread supports string keys in arrays.
Arrow Functions (PHP 7.4+)
Op Name Example
=> Arrow function fn($n) => $n * 2
=> Array pair ['key' => 'value']
Arrow functions: Auto-capture parent scope by value. Single expression, implicit return.
Type Declaration Operators (PHP 8.0+)
Op Name Example
| Union type int|float $num
& Intersection type Countable&Traversable
PHP 8.2+: DNF types combine | and &: (A&B)|C
Match Expression (PHP 8.0+)
Keyword Description Example
match Expression-based switch with strict comparison and return value. Throws UnhandledMatchError if no match. $result = match($status) { 200 => 'OK', 404 => 'Not Found', default => 'Error' };
First-Class Callables (PHP 8.1+)
Syntax Description Example
... Creates Closure from callable. Works with functions, methods, static methods. $fn = strlen(...); $method = $obj->method(...); $static = User::find(...);
Cast Operators
Op Name Example
(int) Integer cast (int)"42.7" → 42
(float) Float cast (float)"42.7" → 42.7
(string) String cast (string)42 → "42"
(bool) Boolean cast (bool)1 → true, (bool)0 → false, (bool)"" → false
(array) Array cast (array)"test" → ["test"], (array)$obj → array of properties
(object) Object cast (object)['a' => 1] → stdClass {a: 1}
Laravel-Specific Tips
Eloquent relationships: Use ?-> for optional relations: $user?->profile?->bio prevents errors on null. Config defaults: config('app.name') ?? 'Laravel' or ??= for caching. Collections: First-class callables work great: $users->map(trim(...)). Match in controllers: Cleaner than switch for status codes, API responses. Spread in config: Merge arrays elegantly: [...$defaults, ...$overrides].