Skip to main content

Documentation Index

Fetch the complete documentation index at: https://docs.syntblaze.com/llms.txt

Use this file to discover all available pages before exploring further.

An operator in PHP is a symbol or keyword that instructs the Zend Engine to perform a specific mathematical, relational, bitwise, logical, or type-checking operation on one or more operands (values, variables, or expressions) to evaluate and return a single resulting value. Operators are classified by their arity (the number of operands they accept):
  • Unary: Operates on a single operand (e.g., !$a, ++$a).
  • Binary: Operates on two operands (e.g., $a + $b).
  • Ternary: Operates on three operands (e.g., $a ? $b : $c).

Operator Categories and Syntax

1. Arithmetic Operators

Perform standard mathematical operations. The modulo operator (%) strips the fractional part of operands before processing. Crucially, the sign of the modulo result always matches the dividend (the left operand). For example, -5 % 3 evaluates to -2. The exponentiation operator (**) is right-associative.
+$a;      // Identity: Conversion to int or float
-$a;      // Negation: Opposite of $a
$a + $b;  // Addition
$a - $b;  // Subtraction
$a * $b;  // Multiplication
$a / $b;  // Division: Returns float unless both operands are integers (or integer-strings) and evenly divisible, which returns an integer.
$a % $b;  // Modulo (integer remainder). Sign matches $a.
$a ** $b; // Exponentiation

2. Incrementing/Decrementing Operators

Unary operators used to increment or decrement a variable’s value by one. The position of the operator determines whether the operation occurs before or after the value is returned.
++$a; // Pre-increment: Increments $a by one, then returns $a
$a++; // Post-increment: Returns $a, then increments $a by one
--$a; // Pre-decrement: Decrements $a by one, then returns $a
$a--; // Post-decrement: Returns $a, then decrements $a by one

3. Assignment Operators

Bind a value to a variable. The basic assignment operator (=) evaluates to the assigned value, allowing for chained assignments. Compound assignment operators perform an operation and assign the result in a single step.
$a = $b;   // Basic assignment
$a += $b;  // Equivalent to: $a = $a + $b
$a .= $b;  // Equivalent to: $a = $a . $b (String concatenation)
$a ??= $b; // Null coalescing assignment: assigns $b to $a if $a is null

4. Comparison Operators

Evaluate the relationship between two operands, returning a boolean. PHP distinguishes between loose comparison (allows type coercion) and strict comparison (checks both value and type).
$a == $b;   // Equal (loose: type coercion applied)
$a === $b;  // Identical (strict: same value and type)
$a != $b;   // Not equal (loose)
$a !== $b;  // Not identical (strict)
$a < $b;    // Less than
$a >= $b;   // Greater than or equal to
$a <=> $b;  // Spaceship: Returns -1, 0, or 1 (less than, equal, greater than)

5. Logical Operators

Evaluate boolean logic. PHP implements short-circuit evaluation for logical operators. PHP provides two sets of logical operators with different precedence levels (&&/|| have higher precedence than and/or). The and and or operators have lower precedence than the assignment operator (=). This means an expression like $x = true and false; evaluates as ($x = true) and false;, leaving $x assigned to true.
$a && $b;  // True if both are true (High precedence)
$a || $b;  // True if either is true (High precedence)
!$a;       // True if $a is not true
$a and $b; // True if both are true (Lower precedence than '=')
$a or $b;  // True if either is true (Lower precedence than '=')
$a xor $b; // True if either is true, but not both

6. Conditional (Ternary) Operators

Evaluate an expression for a boolean truth value and return one of two subsequent expressions based on that evaluation. PHP also supports a shorthand version known as the Elvis operator, which omits the middle expression.
$a ? $b : $c; // Ternary: Evaluates to $b if $a is true, and $c if $a is false.
$a ?: $c;     // Elvis: Evaluates to $a if $a is true, and $c if $a is false.

7. Bitwise Operators

Evaluate and manipulate specific bits within an integer. If both operands are strings, the operation occurs on the ASCII values of the characters.
$a & $b;  // And: Bits set in both $a and $b are set.
$a | $b;  // Or: Bits set in either $a or $b are set.
$a ^ $b;  // Xor: Bits set in $a or $b, but not both, are set.
~$a;      // Not (Unary): Flips all bits. For signed integers, this results in -$a - 1 due to two's complement.
$a << $b; // Shift left: Shift bits of $a left by $b steps.
$a >> $b; // Shift right: Shift bits of $a right by $b steps.

8. Type Operator

Determines if a PHP variable is an instantiated object of a specific class, a subclass, or implements a specific interface.
$a instanceof ClassName; // True if $a is an object of ClassName or its descendants.

9. Null-Handling Operators

Provide concise syntax for evaluating null states without triggering undefined variable warnings or fatal errors.
$a ?? $b;       // Null Coalescing: Returns $a if it exists and is not null; otherwise $b.
$a?->method();  // Nullsafe: Evaluates method() if $a is not null; otherwise returns null.

10. String and Array Operators

PHP uses specific operators for string concatenation and array union, distinct from arithmetic addition. Both the string concatenation and array union operators return a new value without modifying the original operands in-place.
// String
$a . $b;   // Concatenation: Returns a new string combining $a and $b.

// Array
$a + $b;   // Union: Returns a new array merged by keys. Elements in $b whose keys already exist in $a are ignored.
$a == $b;  // Equality: True if $a and $b have the same key/value pairs.
$a === $b; // Identity: True if same key/value pairs in the same order and types.

11. Execution and Error Control

Specialized operators for system-level execution and error suppression.
`command`; // Execution: Executes shell command and returns output (backticks).
@$a;       // Error Control: Suppresses diagnostic errors generated by expression $a.

Precedence and Associativity

When multiple operators are present in an expression, precedence dictates the order of evaluation (e.g., * is evaluated before +). When operators have equal precedence, associativity dictates the evaluation direction:
  • Left-associative: Evaluated from left to right (e.g., -, .).
  • Right-associative: Evaluated from right to left (e.g., =, **).
  • Non-associative: Cannot be chained together (e.g., <=>, ==, and as of PHP 8.0, the ternary operator ? :).
Master PHP with Deep Grasping Methodology!Learn More