Google News
logo
PHP Program to Increment/Decrement Operators
In the following example of PHP program that demonstrates the use of increment and decrement operators :
Program :
<?php

$a = 5;

// Post-increment operator
$b = $a++;
echo "Post-increment operator: a = $a, b = $b <br>";

// Pre-increment operator
$c = ++$a;
echo "Pre-increment operator: a = $a, c = $c <br>";

// Post-decrement operator
$d = $a--;
echo "Post-decrement operator: a = $a, d = $d <br>";

// Pre-decrement operator
$e = --$a;
echo "Pre-decrement operator: a = $a, e = $e <br>";

?>
Output :
Post-increment operator: a = 6, b = 5
Pre-increment operator: a = 7, c = 7
Post-decrement operator: a = 6, d = 7
Pre-decrement operator: a = 5, e = 5
In this example, we have declared a variable `$a` and assigned it the value `5`.

We then use increment and decrement operators to modify the value of `$a` and assign it to other variables `$b`, `$c`, `$d`, and `$e`.

Finally, we print out the results using the `echo` statement.