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 known as the decrement operator. It reduces the numeric value of a variable by exactly one. It modifies the operand in place and evaluates to either the original or the newly decremented value, depending on its syntactical placement relative to the variable. The operator operates in two distinct modes based on its position: 1. Pre-decrement (--$variable) The operator is placed before the operand. PHP decrements the variable by one before evaluating the expression. The operation returns the newly decremented value. 2. Post-decrement ($variable--) The operator is placed after the operand. PHP evaluates the expression using the variable’s current value, and then decrements the variable by one after the evaluation is complete.
// Pre-decrement mechanics
$a = 10;
$b = --$a; 
// Evaluation order:
// 1. $a is decremented to 9
// 2. The new value of $a (9) is assigned to $b
// Result: $a is 9, $b is 9

// Post-decrement mechanics
$x = 10;
$y = $x--; 
// Evaluation order:
// 1. The current value of $x (10) is assigned to $y
// 2. $x is decremented to 9
// Result: $x is 9, $y is 10

Type Handling and Coercion

The -- operator exhibits specific behaviors when applied to non-integer data types:
  • Floats: Decrements the floating-point value by exactly 1.0. For example, 5.5 becomes 4.5.
  • Numeric Strings: If a string contains a valid numeric value (e.g., "10"), PHP implicitly casts it to an integer or float before applying the decrement operation.
  • Non-numeric Strings: Unlike the increment operator (++), the decrement operator does not support Perl-style alphanumeric string manipulation. Applying -- to a non-numeric string has no effect. As of PHP 8.3, this emits an E_WARNING.
  • Booleans: Applying the decrement operator to a boolean (true or false) does not alter its value. As of PHP 8.3, this emits an E_WARNING.
  • Null: Applying the decrement operator to null has no effect and evaluates to null. As of PHP 8.3, this emits an E_WARNING, and it will throw a TypeError in PHP 9.0. (Note: This contrasts with the ++ operator, which increments null to 1).
  • Objects/Arrays: Applying the decrement operator to an array or an object that does not overload the operator will result in a fatal TypeError.
Master PHP with Deep Grasping Methodology!Learn More