Increment & Decrement
The increment operators are used to increment a variable's value.The decrement operators are used to decrement a variable's value.
$x++; // equivalent to $x = $x+1;
$x--; // equivalent to $x = $x-1;
$x--; // equivalent to $x = $x-1;
Increment and decrement operators either precede or follow a variable.
$x++; // post-increment
$x--; // post-decrement
++$x; // pre-increment
--$x; // pre-decrement
$x--; // post-decrement
++$x; // pre-increment
--$x; // pre-decrement
The difference is that the post-increment returns the original value before it changes the variable, while the pre-increment changes the variable first and then returns the value.
Example:
$a = 2; $b = $a++; // $a=3, $b=2
$a = 2; $b = ++$a; // $a=3, $b=3
$a = 2; $b = ++$a; // $a=3, $b=3
No comments:
Post a Comment