Reputation: 771
This is propably a farcical "problem" but I just don't see the reason for this behaviour.
Facts:
$i++;
returns the current value and then increments $i by one.
++$i;
increments $i by one and then returns $i.
Situations:
for($i = 0; $i < 10; ++$i){
echo $i."\n";
}
gives
0
1
2
3
4
5
6
7
8
9
2nd:
for($i = 0; $i < 10; $i++){
echo $i."\n";
}
gives also
0
1
2
3
4
5
6
7
8
9
If I'd take the documentation of the increment literally, I would explain the loops as follows:
It's a fact that this is not true. Could somebody please explain, why?
Upvotes: 0
Views: 184
Reputation: 219920
$i
is not being returned, it is being used (by you). Big difference.
If you were to rewrite your if
statements to instead use a while
loop, you'd have these:
Post-increment:
$i = 0;
while ( $i < 10 ) {
echo $i."\n";
$i++;
}
Pre-increment:
$i = 0;
while ( $i < 10 ) {
echo $i."\n";
++$i;
}
As you can see, there's no difference between the two.
Here's what the 3 statements you supply to the for loop are for:
at no point is the returned value of the 3rd statement being utilized in any way.
P.S. As mentioned, the third statement is run after every loop including the last one. This is evident by accessing the $i
variable after the loop has completed:
for ($i = 0; $i < 10; $i++) {
echo $i."\n";
}
echo $i;
which would list all the numbers up to and including 10.
See it here in action: http://viper-7.com/Y6N2jU
Upvotes: 6
Reputation: 3763
As I understand it, the for
loop checks the condition in the second parameter before executing the third parameter. This means that, in both cases:
$i
is initialized with value 0 (this step is obviously skipped for every subsequent iteration);$i
is compared with 10 (it's smaller); and then$i
is incremented.In that last step, whether the value of $i
is returned before or after the increment shouldn't matter, because the "returned" value of $i
isn't actually used in any comparison. The actual value of $i
as stored in memory has still been incremented by one each iteration, after the comparison has been made.
Upvotes: 2
Reputation: 5685
Post-incrementing or Pre-incrementing a variable as the third expression in the loop doesn't have an effect on the behavior of it
Because, $i++
and ++$i
would both execute at the end of each iteration of the loop.
Upvotes: 0
Reputation: 10093
A for loop has 3 components:
The order of execution of these 3 pieces of code is:
(1) executed only once at the beginning of the for loop
(2) verified before each iteration
(3) executed after each iteration
Consequently it's not important if you use the prefix or postfix form of the increment operation; after each iteration the increment (3) will be executed and you will get a new value of the incremented variable
This:
for($i = 0; $i < 10; $a = $i++){
echo $i."\n";
}
as compared to this
for($i = 0; $i < 10; $a = ++$i){
echo $i."\n";
}
would make a difference in the value of $a after each iteration, but not in the value of $i
Upvotes: 1
Reputation: 856
If you write the for loop out as a sequence of if statements, I think it would make more sense as to why $i increments each time.
i = 0
if i > 10
return
else
print i
i++ // or ++i
if i > 10
return
else
print i
i++ // or ++i
and so on.
Upvotes: 0