8

How are they different? Here's what I'm thinking, but I'm not sure....

If you use pre-incrementation, for example in a for loop with ++j, then you are basically saying: "Make a copy of the value of j for use in the loop, then increment j, then go through the statements in the loop with the copy of j." If you are using post-incrementation in the same loop j++, then you are basically saying: "Make a copy of the value of j for use in the loop, then go through the statements in the loop with the copy of j, then increment j."

The reason I'm unsure is because I've created a for loop that multiplies the value of j by 10 and then outputs the result for j=1 through j=12, using both post- and pre-incrementation. The human readable output is exactly the same with post- and pre-incrementation. I'm thinking, 'How are the outputs exactly the same if there isn't some kind of copy operation involved?'

So, I'm guessing the difference between pre- and post-incrementation truly becomes important, in php, when I use references (which act as pointers in php) rather than names for return values? This would be because copies of references aren't made, so pre-incrementation would be: "Increment j, then go through the statements in the loop with the changed value of j, then increment j again...," whereas post-incremetation would look like: "Use the value of j for the statements in the loop, then change the value of j, then go through the loop with the new value of j..."

Wolfpack'08
  • 3,982
  • 11
  • 46
  • 78
  • 5
    This behavior is language agnostic (or at least, not specific to PHP), so this is really a duplicate of [Why doesn't changing the pre to the post increment at the iteration part of a for loop make a difference?](http://stackoverflow.com/questions/1918196/why-doesnt-changing-the-pre-to-the-post-increment-at-the-iteration-part-of-a-for) – Mark Elliot Jun 19 '11 at 04:23
  • 3
    in addition to what mark says, this question suggests you have a fairly fundamental misunderstanding of what a for loop actually does... there is no copying involved whatsoever - the 3rd clause of a for loop is simply executed at the end of the body of the loop. you can put whatever statmenet you like in there and no special copying will occur – tobyodavies Jun 19 '11 at 04:35
  • 1
    And no references do not act as pointers. – netcoder Jun 19 '11 at 04:45
  • @tobyodavies: While I think the misunderstanding is also with how incrementation works, I feel I should also look up looping behavior due to your post. Thank you. Any good resource? :) Also, I understand your comment "...no special copying will occur," because of another user's answer, which I have selected as being correct. Thanks, again. – Wolfpack'08 Jun 22 '11 at 14:15
  • @netcoder: I disagree because php 5.3 references actually are pointers. – Wolfpack'08 Jun 22 '11 at 14:16
  • 2
    @Fohsap: Nope, all variables in PHP are pointers (since PHP4): the `zval*`. A pointer points to a specific memory location. There is no such thing in PHP, whether it's 5.2 or 5.3. There are references however, which points to entries within PHP symbol tree, but you still can't make them point to a memory location of your choice. So again, no, references are not and do not act as pointers. – netcoder Jun 22 '11 at 14:27
  • 1
    @netcoder +1 - my litmus test for references vs pointers is "can you do pointer arithmetic?" in php you clearly can't. – tobyodavies Jun 24 '11 at 02:29
  • @netcoder But in the practical sense, they are. – Wolfpack'08 Feb 22 '13 at 07:08
  • @MarkElliot This has a better answer, and it targets a different learning style. – Wolfpack'08 Feb 22 '13 at 07:10

3 Answers3

30

Pre- or post-incrementing do not magically delay things until later. It's simply inline shorthand.

enter image description here

// pre-increment
$var = 5;
print(++$var); // increments first, then passes value (now 6) to print()

// post-increment
$var = 5;
print($var++); // passes value (still 5) to print(), then increments

Now let's look at a loop.

for ($i = 0; $i < 9; $i++) {
    print($i);
}

The last part of the loop declaration (the $i++) is simply the statement to execute after each time through the loop. It "passes" the value to nowhere, then increments it. $i isn't used anywhere at that time. Later when the next statement is executed (print($i);), the value of $i has already increased.

// add 1, then do nothing with $i
for ($i = 0; $i < 9; ++$i) {}

// do nothing with $i, then add 1
for ($i = 0; $i < 9; $i++) {}

Whichever way you do it, $i will be the same within the loop.


If it helps, you can think of them as small routines that kind of do this:

// ++$i
{
    $i = $i + 1;
    return $i;
}

// $i++
{
    return $i;
    $i = $i + 1;
}

As I reread your question, I think the confusion is more with how the loop works than how increment operators work. Keeping in mind that the increment is a straightforward, all-at-once operation, here's how third expression in the loop works.

// here's a basic loop
for ($i = 0; $i < 9; $i++) {
    // do loop stuff
    print($i);
}

// this is exactly what happens
for ($i = 0; $i < 9; ) {
    // do loop stuff
    print($i);

    $i++;
}

Just because that last line can be put in the loop declaration doesn't give it any special powers. There are no references or anything used behind the scenes. The same $i variable is seen both inside and outside the loop. Every statement inside or outside the loop directly looks up the value of $i when necessary. That's it. No funny business.

Wiseguy
  • 20,522
  • 8
  • 65
  • 81
  • So basically it's because it's for the return values rather than the received values? – Wolfpack'08 Jun 19 '11 at 04:35
  • @Fohsap I'm thinking your confusion is more with the loop than with the incrementing. See my latest update. I hope that helps. – Wiseguy Jun 19 '11 at 05:02
1

When doing $x++, you are post-incrementing... This means that the incrementation will only occur after the statement has been evaluated.

So, given the following code:

$x = 10; $y = 0; $z = 5;

$y = $z * $x++;

PHP does this:

$x = 10; $y = 0; $z = 5;

$y = $z * $x++;

// Ignore Post-Increment, Evalutate
$y = $z * $x;
$y = 5 * 10;

// Now Increment x - POST-INCREMENT
$x = $x + 1;
$x = 10 + 1;
$x = 11;

// Continue evaluating statement
$y = 5 * 10;
$y = 50;

When doing ++$x, you are pre-incrementing... This means that the incrementation will occur before the statement is evaluated:

$x = 10; $y = 0; $z = 5;

$y = $z * ++$x;

// Do Pre-Increment
$x = $x + 1;
$x = 10 + 1;
$x = 11;

// Evaluate
$y = $z * $x;
$y = 5 * 11;
$y = 55;

In the case of a for loop in PHP, PHP evaluates a for loop as follows:

for($i = 0; $i < 30; $i++) {
  doSomething();
}

// Is evaluated EXACTLY as such by PHP

$i = 0;
while($i < 30) {
  doSomething();

  $i++;
}

The first expression ($i = 0) is evaluated (executed) once unconditionally at the beginning of the loop.

In the beginning of each iteration, $i < 30 is evaluated. If it evaluates to TRUE, the loop continues and the nested statement(s) are executed. If it evaluates to FALSE, the execution of the loop ends.

At the end of each iteration, $i++ is evaluated (executed) as an independent expression.

Therefore, post-incrementing or pre-incrementing a variable as the third expression in the loop doesn't have an effect on the behavior of it. In this simple case, both expressions will behave exactly the same.

However, in a complex loop such as the following:

for($i = $j = 0; $i < 30; $i += ++$j) {
  $j = getResult($j);
}

Post-incrementing or pre-incrementing $j directly affects the value of $i according to the examples above. In this case, you need to choose exactly what you want to do.

Andrew Moore
  • 93,497
  • 30
  • 163
  • 175
-1
$i = 0;
echo $i++;
echo $i;
$j=0;
echo ++$j;
echo $j;

Pre increment display incremented value. But Post increment display value then increment. About code will output 01 and 11

Kaja Mydeen
  • 585
  • 1
  • 7
  • 13