Increments and decrements in PHP

Views: 28 Last modified: August 03rd, 2011 Comments: 0

Below, some easy and quite simple examples of increments and decrements!

Post in/decrement:
Those adjustments are following a simple rule… First show the count, then add/substract 1.

Pre in/decrement:
Those adjustments are following a simple rule… First add/substract 1, then show the count.

Increment

// Example 1 (post increment) First show, then add 1
// Start number
$count = 0;

// Result: 0
echo $count++;

// Just to prove the result will be 1 finally....
echo $count;

// Example 2 (pre-increment) First add 1, then show
// Start number
$count = 0;

// Result: 1
echo ++$count;

Decrement

// Example 1 (post decrement) First show, then substract 1
// Start number
$count = 10;

// Result: 10
echo $count--;

// Just to prove the result will be 9 finally....
echo $count;

// Example 2 (pre-decrement) First substract 1, then show
// Start number
$count = 10;

// Result: 9
echo --$count;
VN:F [1.9.13_1145]
Rating: 0.0/10 (0 votes cast)
VN:F [1.9.13_1145]
Rating: 0 (from 0 votes)

    Mail this!

    To: From:Sum {1+4} =  
    Anything to add ?

        You must be logged in to post a comment.