Limited Offer
60% OFF on all plans!

Operators in PHP

Complete guide to PHP operators: arithmetic, assignment, comparison, logical, string, bitwise, ternary, null coalescing, spaceship, and operator precedence.

Arithmetic Operators

Arithmetic operators perform basic math. PHP supports addition, subtraction, multiplication, division, modulo, and exponentiation.

1. + - * / % **

The six core arithmetic operators.

2. Increment and Decrement (++ --)

Pre/post increment and decrement operators.

Assignment Operators

PHP supports the basic `=` assignment plus compound operators that combine an operation with assignment, reducing verbosity.

1. = Basic Assignment

Assigns the right-hand value to the left-hand variable.

2. Compound Assignment Operators

Shorthand for combining an operation with assignment.

3. Null Coalescing Assignment (??=) — PHP 7.4+

Assigns a value only if the variable is null or not set.

Comparison Operators

PHP has both loose (`==`) and strict (`===`) comparison. Always prefer strict comparison to avoid unexpected type-juggling results.

1. == vs === (and != vs !==)

Loose equality juggles types; strict equality requires same type AND value.

2. < > <= >=

Relational operators compare numeric or string order.

3. Spaceship Operator (<=>) — PHP 7.0+

Returns -1, 0, or 1 — designed for sorting callbacks.

Logical Operators

PHP provides two sets of logical operators: symbolic (`&&`, `||`, `!`) and keyword (`and`, `or`, `not`). They behave identically except for operator precedence — the symbolic versions have higher precedence.

1. && || !

Symbolic logical operators — high precedence.

2. Short-circuit Evaluation

PHP stops evaluating as soon as the result is determined.

3. and, or, not — Keyword Operators

Same behaviour as `&&`, `||`, `!` but with very low precedence — lower than assignment.

String Operators

PHP uses `.` for string concatenation — not `+`. This is one of the biggest differences from JavaScript.

1. . and .= Operators

`.` joins two strings; `.=` appends to a variable.

Null Coalescing & Ternary Operators

PHP provides three shorthand conditional operators: the ternary `?:`, the short ternary (Elvis) `?:`, and the null coalescing `??` (PHP 7.0+).

1. Ternary Operator (?:)

`condition ? valueIfTrue : valueIfFalse` — inline if-else.

2. Elvis Operator (?:) — Short Ternary

Omit the middle value to return the condition itself if truthy.

3. Null Coalescing Operator (??) — PHP 7.0+

Returns the left side if it is set and not null; otherwise returns the right side.

Bitwise Operators

Bitwise operators work on the individual bits of an integer. They are used in permission systems, flag management, and low-level data processing.

1. Binary Representation

Understanding bits is essential before using bitwise operators.

2. & | ^ ~ — AND, OR, XOR, NOT

The four core bitwise operators.

3. << >> — Left and Right Shift

Shift bits left or right — equivalent to multiplying or dividing by powers of 2.

4. Practical: Permission Flags

A classic real-world use of bitwise operators — combining and checking permission flags.

Type Operators

PHP provides `instanceof` for object type checking and `gettype()` / `is_*()` functions. The `match` expression (PHP 8.0+) is a clean alternative to type-based switches.

1. gettype() — Get the Type Name

Returns the type of a variable as a lowercase string.

2. instanceof

Check if an object is an instance of a class or interface.

Operator Precedence

Operator precedence determines which operation happens first when multiple operators appear in the same expression. When in doubt, use parentheses — they always take top priority.

1. Precedence in Action

Higher-precedence operators run first without parentheses.

2. PHP Operator Precedence Table

From highest to lowest — higher runs first.