Learn PHP-17 : Incremental and Decremental Operators

Posted: November 8, 2010 in PHP
Tags:

Incrementing and Decrementing operators are very widely used in most programming languages. They allow a PHP programmer to increment and decrement values as needed. Many times they are used in a loop to keep track of the index key number in the loop’s array. We discuss loops in detail in later sections.

The following table shows their usage and result logic:

Name Usage Result
Pre-increment ++$myVar Increments $myVar by one
Post-increment $myVar++ Returns $myVar, then increments $myVar by
one
Pre-decrement $myVar Decrements $myVar by one, then returns $myVar
Post-decrement $myVar Returns $myVar, then decrements $myVar by
one

Here is a code example of incrementing a variable’s value by one:

<?php $myVar = 3; // Use Post-increment to increment the value by 1 $myVar++; // Now display to browser or use its new value in script echo $myVar; ?>

When browse it is shown like below :

4

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s