PHP Increment Decrement Operator


 

PHP Increment Decrement Operator

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.

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

Ads