Share on Google+Share on Google+

PHP Increment Decrement Operator

Advertisement
In this tutorial we will study about increment and decrement operators, PHP follows 'C' style increment and decrement operators. Examples in this tutorial will make it more clear.

PHP Incrementing and Decrementing variables:

PHP follows very common style of increment and decrement, much like C,C++. but in few cases it follows Perl style, e.g. if we perform  'Z'+1 then PHP will display 'AA' whereas in 'C' and 'Java' result would be '['.

Examples:

<?php

$a=12;

echo "Pre increment value of a is:".++$a."<br/>";

echo "Post increment value of a is:".$a++."<br/>";

echo "After post increment value of a is:".$a."<br/>";

echo "Pre decrement value of a is:".--$a."<br/>";

echo "Post decrement value of a is:".$a--."<br/>";

echo "After post decrement value of a is:".$a--."<br/>";

?>

Output:

Pre increment value of a is:13
Post increment value of a is:13
After post increment value of a is:14
Pre decrement value of a is:13
Post decrement value of a is:13
After post decrement value of a is:12

Advertisements

Advertisement

Posted on: March 19, 2010 If you enjoyed this post then why not add us on Google+? Add us to your Circles

Share this Tutorial Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.