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.

The -- operator is a unary arithmetic operator in PHP used to decrement the numeric value of a variable by exactly one. It modifies the variable in place and evaluates to either the original or the decremented value, depending on its placement relative to the operand. The operator operates in two distinct modes based on syntax positioning: 1. Pre-decrement (--$variable) The PHP engine subtracts 1 from the variable’s value, updates the variable in memory, and then returns the newly updated value to the evaluating expression. 2. Post-decrement ($variable--) The PHP engine reads the variable’s current value and caches it, subtracts 1 from the variable in memory, and then returns the cached (original) value to the evaluating expression.
// Pre-decrement evaluation
$a = 10;
$b = --$a; 
// Memory state: $a is 9
// Expression result: $b is assigned 9

// Post-decrement evaluation
$x = 10;
$y = $x--; 
// Memory state: $x is 9
// Expression result: $y is assigned 10

Type Coercion and Edge Cases

The -- operator applies specific type-handling rules when the operand is not a strict integer or float:
  • Numeric Strings: Strings containing valid numeric values (e.g., "5") are implicitly cast to integers or floats before the decrement operation is applied.
  • Non-Numeric Strings: Unlike the increment operator (++), which supports Perl-style string increments, the decrement operator does not affect the value of non-numeric strings. As of PHP 8.3, applying -- to a non-numeric string emits an E_DEPRECATED warning (slated to become a TypeError in PHP 9.0). In older versions, the operation silently does nothing.
  • Booleans: Decrementing a boolean (true or false) has no effect on the variable’s value. However, as of PHP 8.3, performing this operation emits an E_DEPRECATED warning.
  • Null: Decrementing a null value has no effect, and the variable remains null. As of PHP 8.3, this operation emits an E_DEPRECATED warning.
  • Objects/Arrays: Applying the decrement operator to an array or an object that does not overload the operation will result in a TypeError.
Master PHP with Deep Grasping Methodology!Learn More