Jump to content

What is the deifference between Post Increment and Pre Increment?


phpcharan

Recommended Posts

Try running the following two code snippets:

 

$foo = 6;
$bar = 5;

if($foo == $bar++){
echo 'True. Foo:'.$foo.' Bar:'.$bar;
}else{
echo 'False. Foo:'.$foo.' Bar:'.$bar;
}

 

$foo = 6;
$bar = 5;

if($foo == ++$bar){
echo 'True. Foo:'.$foo.' Bar:'.$bar;
}else{
echo 'False. Foo:'.$foo.' Bar:'.$bar; 
}

 

As you will see, in both cases, after the if statement, both $foo and $bar are 6. However, only the if statement in the second code snippet is true. That is because $bar is incremented prior to being tested for equality.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.