0% complete

Operators · Lesson 5 of 6

Increment & Decrement

Increase or decrease a numeric variable by one.

Lesson

The ++ and -- operators adjust a number by one. Prefix changes the value before the expression is evaluated; postfix evaluates the old value first. A standalone increment in a loop is easy to read, but avoid mixing prefix and postfix behavior into complicated expressions. This example uses a conventional loop counter whose update is easy to follow. Further guidance Evaluate an expression from its grouped parts and add parentheses whenever precedence is not obvious. Check divisors before division, be cautious with floating-point equality, and avoid changing state inside a complicated expression. Small, named intermediate values make calculations easier to verify. Practice task: Create a fresh example for Increment & Decrement. Predict the result before running it, then change one input and explain why the result changes. Test a normal case and one boundary or invalid case where it applies. Compare your output with the example and note one rule you would use in a real project.

Code examples

Increment & Decrement
PHP
$attempts = 0;
$attempts++;
echo $attempts;
Expected output
1

Key points

  • Increase or decrease a numeric variable by one.
  • Make calculation order explicit with parentheses and named intermediate values.
  • Check edge cases such as zero divisors and floating-point precision.

Your learning progress

Finish this lesson

Optional practice

PHP playground

This site does not execute learner-provided PHP code. Connect a secured PHP sandbox service before enabling code execution.

Execution is disabled. No code is sent to a server.