|
Posted by Tyno Gendo on 04/12/07 08:28
peter wrote:
>> it makes ALL the difference. the proof is using ++$imagenum rather than
>> $imagenum++. even if this were true and all there was to it, there is
>> incrementation involved. $imagenum after the assignment should be 1 more
>> than it was before the assignment! and in a loop, each pass would be 1
>> greater than the last. as it is, it remains the same always.
>>
>> ++$imagenum considers the current value of $imagenum before the
>> right-hand-side operation, increments, and then sets the left-hand-side
>> variable ($imagenum). this will return correct/expected results.
>>
>> $imagenum++ considers the value of $imagenum AFTER the rhs operation,
>> however since $imagenum IS the lhs variable being set, it cannot be
>> determined to what $imagenum should be set from the rhs operation...so,
>> neither operation is successful. the assignment and rhs post-increment
>> effectively cancel eachother out.
>>
>> it is not just assigning itself to itself...that would be $imagenum =
>> $imagenum. it is also incrementing itself after the assignment. logically,
>> it is:
>>
>> $i = $i;
>> $i++;
>>
>> however, it is written such that the value of $i is indeterminate...so
>> what's there to increment...or to what?
>
> I was referring to the way it was used by the previous op. The line in the
> code was $imagenum = $imagenum++; and someone advised him to use
> $imagenum++; instead and in the way it was being used it did not make any
> difference. If it had been used in the while statement and not in it's body
> then yes it would have made a difference but in this case it did not as I
> stated.
>
>
A discussion relating to this has been going on in comp.lang.php (where
I made a fool of myself, but hey, we are all learning ;) )
Anyhows, turns out
$imagenum = $imagenum++
The ++ operation returns the variables original value, operating much
like a function, see PHP manual pages:
http://www.phpbuilder.com/manual/en/language.operators.increment.php
$a++ Post-increment Returns $a, then increments $a by one.
The key bit is that the increment first "returns $a"
Then the increment operator seemingly doesn't affect the $imagenum you
have assigned to.
So to me, that says that rather then return the result returns is a
"copy" of the original $imagenum's original value, not a reference to
$imagenum, and the ++ is effectively lost.
In short, don't bother to try and understand it, just do $imagenum+=1
instead.
Navigation:
[Reply to this message]
|