Star Hoster
Photoshop Tutorials
Anonymous Web Surfing
Funny Jokes
Myspace Friends
Open Treasure Chest
Building in Paradise


July 6, 2006

Pre/Post Increment, Decrement

Filed under: Intro PHP Tutorials — phpdeveloper @ 2:06 pm

Pre/Post Increment, Decrement

We touched on these features in PHP before, now lets go a little deeper into the operation of these functions.

Examples

$index++; //Post-increment, Give the value of index, then increments index by one
++$index; // Pre-increment, increments index by one, then returns the valueof index
$index–; // Post-decrement, Give the value of index, then decreases index by one
–$index; // Pre-drecrement, decreased increments index by one, then returns the value of index

Coded Example

< ?php
echo "Post-increment";
$index = 10;
echo "Index value(10): " . $index++. "\n";
echo "Index value(11): " . $index . "\n";

echo "Pre-increment";
$index = 10;
echo "Index value(11): " . ++$index. "\n";
echo "Index value(11): " . $index . "\n";

echo "Post-decrement";
$index = 10;
echo "Index value(10): " . $index--. "\n";
echo "Index value(9): " . $index . "\n";

echo "

Pre-decrement”;
$index = 10;
echo “Index value(9): ” . –$index. “\n”;
echo “Index value(9): ” . $index . “\n”;

?>

This little features, can come in very hand, so always look for ways to simplify your code and make things easier for yourself. Pre/Post decrement and increment are very useful in for loop and counters.

No Comments »

No comments yet.

RSS feed for comments on this post. TrackBack URI

Leave a comment

You must be logged in to post a comment.