PHP Increment and Decrement Operators

But I don’t wanna Decrement.

PHP increment and decrement operators: these are operators that either add 1 or subtract 1 from the current value. The operators can be prefixed or post-fixed and will yield slightly different results each time.

  • Increment Operator: ++
  • Decrement Operator: —

Normally, to add 1 to a value, you would use the addition operator.

 

<?php$a = 5;
$a = $a + 1;
var_dump($a);?>

 

In the example above, you evaluate the expression on the right, $a + 1, which becomes 6, and then you assign the value back to $a, on the left.

With the postfix-increment operator, we can append ++ to the end of the variable and it will do the same operation.

 

<?php$b = 5; 
var_dump($b++); // still displays 5
var_dump($b); // displays 6?>

 

But if we evaluate the variable, we get an unexpected result: 5. If we were adding 1 to the value, why does it still display 5 and not 6? With the postfix-increment operator, the value is displayed first, and then the operation is performed. If we call the value after, it will display the previous value plus 1.

With the prefix-increment operator, the expression is evaluated first, and is then displayed.

 

<?php$c = 5;
var_dump(++$c); // displays 6
var_dump($c); // still displays 6?>

 

You only have to keep this in mind if you’re performing the operation inside of an expressions or passing the variable as an argument. If you increment the value and then call it, the result will be the same regardless of whether you prefix or post-fix the operator.

 

<?php$a = 5;
$a++;
echo $a; // prints 6$b = 5;
++$b;
echo $b; // prints 6$c = 5;
echo $c++; // prints 5
echo $c; // prints 6$d = 5;
echo ++$d; // prints 6?>

 

The same principle applies to the decrement operator, but instead of adding 1 to the value, you subtract 1.

 

<?php$a = 5;
$a--;
echo $a; // prints 4$b = 5;
--$b;
echo $b; // prints 4$c = 5;
echo $c--; prints 5
echo $c; prints 4$d = 5;
echo --$d; // prints 4?>

 

Increment and decrement operators are used heavily in loops, which we haven’t gotten to yet, but will soon. Next we’ll look at logical operators.

 

 

PHP Tutorial Series

Continue your learning with these articles

PHP Comparison Operators

So You Want to Compare Stuff?

PHP – P17: Comparison Operators

PHP Comparison Operators, or relational operators, test some type of relation between two values. Greater-than, less-than, is equal to. These are just a few.

PHP Increment and Decrement Operators

But I don’t wanna Decrement.

PHP – P18: Increment and Decrement Operators

These are operators that either add 1 or subtract 1 from the current value. The operators can be prefixed or post-fixed and will yield slightly different results each time.

PHP Logical Operators

LOGIC DICTATES.

PHP – P19: LOGICAL OPERATORS

You might have even been exposed to logic in Philosophy. If you’re not familiar with logic, this may be a bit of a shock for you. Only joking. It’s just another operator in our disposal that evaluates to either true or false.

Leave a Reply