PHP Operators - Incremental Operators
(Page 4 of 6 )
Sometimes you want to increase or decrease the value of a variable by one. This is especially useful when using Loops, which we will cover in another tutorial. Here is an example of how you would work with them:
<html>
<body>
<?php
$value=9;
echo $value;
echo ++$value;
?>
</body>
</html>
The above code results in this print-out:
9
10
When the ++ appears before the variable it adds a one to it. If a –- appears before a variable it subtracts one from it.
Let's make it a little trickier. Consider the following code:
<html>
<body>
<?php
$value=9;
echo $value;
echo $value++;
?>
</body>
</html>
This code would result in:
9
9
That is because when we place the ++ after the variable name, it increments the value after the line of code is executed.
<html>
<body>
<?php
$value=9;
echo $value;
echo $value++; // increments the value by one after the line
echo $value; // prints the new value, which is now ten
?>
</body>
</html>
This code prints:
9
9
10
Next: Better by Comparison >>
More PHP Articles
More By James Payne