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 specific symbol or keyword that instructs the Zend Engine to perform a mathematical, relational, bitwise, or logical operation on one or more operands (values or variables) to produce a single resulting value. Operators are the fundamental constructs used to evaluate expressions and mutate state during script execution. Operators are structurally classified by their arity (the number of operands they accept):
  • Unary: Operates on a single operand (e.g., !$a).
  • Binary: Operates on two operands (e.g., $a + $b).
  • Ternary: Operates on three operands (e.g., $a ? $b : $c).

Arithmetic Operators

Perform standard mathematical operations. PHP automatically handles type conversion between integers and floats during evaluation.
+$a;      // Identity (converts to int or float)
-$a;      // Negation
$a + $b;  // Addition
$a - $b;  // Subtraction
$a * $b;  // Multiplication
$a / $b;  // Division (returns float if not evenly divisible)
$a % $b;  // Modulo (operands are cast to int before processing)
$a ** $b; // Exponentiation

Assignment Operators

Bind a value to a variable. The base assignment operator (=) evaluates to the assigned value, allowing chained assignments.
$a = $b;   // Basic assignment
$a += $b;  // Addition assignment (syntax applies to -, *, /, %, **, ., &, |, ^, <<, >>)
$a ??= $b; // Null coalescing assignment (assigns $b to $a if $a is null)

Comparison Operators

Evaluate the relationship between two operands, returning a boolean.
$a == $b;  // Equal (type juggling/coercion applied)
$a === $b; // Identical (strict type and value equality)
$a != $b;  // Not equal (alternative syntax: <>)
$a !== $b; // Not identical
$a < $b;   // Less than
$a > $b;   // Greater than
$a <= $b;  // Less than or equal to
$a >= $b;  // Greater than or equal to
$a <=> $b; // Spaceship (returns int: -1 if <, 0 if ==, 1 if >)

Logical Operators

Evaluate boolean logic. PHP implements short-circuit evaluation for these operators.
!$a;       // Logical NOT
$a && $b;  // Logical AND (high precedence)
$a || $b;  // Logical OR (high precedence)
$a and $b; // Logical AND (low precedence, evaluates after assignment operators)
$a or $b;  // Logical OR (low precedence)
$a xor $b; // Exclusive OR (true if either $a or $b is true, but not both)

Bitwise Operators

Perform operations on the binary representations of integer or string operands. When applied to strings, the bitwise operators evaluate the ASCII values of the characters.
~$a;       // Bitwise NOT (inverts bits)
$a & $b;   // Bitwise AND
$a | $b;   // Bitwise OR (inclusive)
$a ^ $b;   // Bitwise XOR (exclusive)
$a << $b;  // Shift left (shifts $a's bits $b steps to the left)
$a >> $b;  // Shift right (shifts $a's bits $b steps to the right)

String Operators

Handle string concatenation.
$a . $b;   // Concatenation
$a .= $b;  // Concatenation assignment

Array Operators

Evaluate and manipulate arrays. The union operator (+) behaves uniquely by appending the right-hand array to the left-hand array; if keys exist in both arrays, the elements from the left-hand array are preserved, and the duplicate keys from the right-hand array are ignored.
$a + $b;   // Union (combines arrays, ignoring duplicate keys from $b)
$a == $b;  // Equality (true if $a and $b have the same key/value pairs)
$a === $b; // Identity (true if $a and $b have the same key/value pairs in the same order and of the same types)
$a != $b;  // Inequality (alternative syntax: <>)
$a !== $b; // Non-identity

Increment/Decrement Operators

Mutate a variable’s value by one. The position of the operator determines whether the value is returned before or after the mutation.
++$a;      // Pre-increment (increments, then returns $a)
$a++;      // Post-increment (returns $a, then increments)
--$a;      // Pre-decrement (decrements, then returns $a)
$a--;      // Post-decrement (returns $a, then decrements)

Error Control and Execution Operators

Specialized operators for environment and error handling.
@$a;       // Error control (suppresses diagnostic errors/warnings for the specific expression)
`$a`;      // Execution (executes contents as a shell command, identical to shell_exec())

Conditional and Null-Handling Operators

Provide shorthand syntax for control structures and null-state checks.
$a ? $b : $c; // Ternary (evaluates $b if $a is true, otherwise $c)
$a ?: $c;     // Elvis (evaluates $a if $a is true, otherwise $c)
$a ?? $b;     // Null coalescing (evaluates $a if it exists and is not null, otherwise $b)
$a?->b;       // Nullsafe (evaluates method/property 'b' if $a is not null, otherwise returns null)

Type Operator

Evaluates object inheritance and interface implementation.
$a instanceof ClassName; // Returns true if $a is an object of ClassName, its descendants, or implements the specified interface

Precedence and Associativity

When multiple operators are present in a single expression, PHP relies on precedence and associativity rules:
  • Precedence: Determines the grouping of operators with different priorities (e.g., ** binds tighter than *, which binds tighter than +).
  • Associativity: Determines the evaluation direction for operators of equal precedence. Most arithmetic operators are left-associative ($a - $b - $c evaluates as ($a - $b) - $c), but the exponentiation operator (**) is right-associative (2 ** 3 ** 2 evaluates as 2 ** (3 ** 2)). Assignment operators are also right-associative ($a = $b = $c evaluates as $a = ($b = $c)).
  • Non-associativity: Certain operators cannot be chained. Notably, as of PHP 8.0, the ternary operator (? :) is non-associative. Chaining ternary operators without explicit parentheses results in a fatal error.
Master PHP with Deep Grasping Methodology!Learn More