In PHP, the and -- operators are used for incrementing or decrementing variables.
Increment Operator ( )
Decrement Operator (--)
Usage
These operators can be used before or after the variable to achieve different results:
Example
Consider the following code:
$count = 10; echo "Before: $count\n"; echo "Pre-increment: " . ++$count . "\n"; echo "Post-increment: " . $count++ . "\n"; echo "Pre-decrement: " . --$count . "\n"; echo "Post-decrement: " . $count-- . "\n";
Output
Before: 10 Pre-increment: 11 Post-increment: 11 Pre-decrement: 10 Post-decrement: 10
This demonstrates how the increment/decrement operators can be used in different scenarios. Notice that in the post-increment and post-decrement cases, the original value of the variable is used first before being modified.
The above is the detailed content of What's the Difference Between Pre- and Post-Increment/Decrement Operators in PHP?. For more information, please follow other related articles on the PHP Chinese website!