Expressions And Operators: Incrementing And Decrementing

Hack provides ++ and -- syntax for incrementing and decrementing numbers.

The following are equivalent:

$i = $i + 1;
$i += 1;
$i++;
++$i;

Similarly for decrement:

$i = $i - 1;
$i -= 1;
$i--;
--$i;

This is typically used in for loops:

for ($i = 1; $i <= 10; $i++) {
  // ...
}

Note that ++ and -- are statements, not expressions. They cannot be used in larger expressions.

$x = 0;
$y = $x++; // Parse error.

Instead, the above code must be written as statements.

$x = 0;
$y = $x + 1;
$x++;
Was This Page Useful?
Thank You!
Thank You! If you'd like to share more feedback, please file an issue.