RE: RES: [PHP] inexplicable behaviour SOLVED
| From: | Ford, Mike | Date: | Tue, 28 Apr 2009 09:49:06 +0000 |
| Subject: | RE: RES: [PHP] inexplicable behaviour SOLVED | ||
| Groups: | php.general | ||
| Request: | Send a blank email to php-general+get-292063@lists.php.net to get a copy of this message | ||
On 27 April 2009 14:21, PJ advised:
> Ford, Mike wrote:
>> On 26 April 2009 22:59, PJ advised:
>>
>>
>>> kranthi wrote:
>>>
>>>> if $Count1 is never referenced after this, then certainly this
>>>> assignment operation is redundent. but assignment is not the ONLY
>>>> operation of this statement. if u hav not noticed a post increment
>>>> operator has been used which will affect the value of $Count as
well,
>>>> and this operation is required for the script to work.
>>>>
>>>> the script should work even if u replace
>>>> $Count1 = $Count++;
>>>> with
>>>> $Count++;
>>>>
>>>> Kranthi.
>>>>
>>>>
>>>>
>>> Not quite, since that would change the $Count variable and that is
used
>>> leater in the code.
>>>
>>
>> Um -- I must be missing something here, because those two statements
>> have exactly the same effect on $Count, incrementing it by one (and
you
>> said the first statement fixed your problem, so logically the second
one
>> must too).
>>
>> In fact, because you've used a post-increment, the statement
>>
>> $Count1 = $Count++;
>>
>> ends up with $Count == $Count1+1, and $Count1 being the original
value
>> of $Count!!
>>
>> This whole scenario smacks to me of a classic "off-by-one" error --
>> either $Count actually *needs* to be one greater than the value you
>> first thought of, or some other value you are comparing it to should
be
>> one smaller than it actually is.
>>
>> Cheers!
>>
>>
> Thanks for the clarification, Mike. In my ignorance, I was under the
> impression that the right side of the equation was only for the use of
> the left part. How stupid of me. So what I should have been doing was
> $Count1 = $Count + 1; right?
No -- because, as you stated, $Count1 was not referred to anywhere else,
so there was and would have been no usefulness in assigning any value to
it. You could equally well have said $Count1 = 2.71828 + 3.14159 *
$Count++; and got the same result -- because this still increments
$Count by 1, and anything else the statement does is irrelevant if
$Count1 is never referred to anywhere else!!! ;) ;)
What you did worked because $Count++ *always* adds one to the value of
$Count, no matter where it appears -- the crucial part of your original
statement was the $Count++ bit, with the assignment to $Count1 being a
massive red herring.
For ultimate clarification, the following 4 statements all have
identical effect:
$Count = $Count + 1;
$Count += 1;
$Count++;
++$Count;
and any one of them would have had the same effect on your result. This
is why I say it was an off-by-one error -- your programming logic
somehow *needed* $Count to be 1 greater than you thought it did, so the
inadvertent incrementation of it was doing the trick for you.
Hope this helps increase the illumination in your head a tiny bit more.
Cheers!
Mike
--
Mike Ford, Electronic Information Developer,
C507, Leeds Metropolitan University, Civic Quarter Campus,
Woodhouse Lane, LEEDS, LS1 3HE, United Kingdom
Email: m.ford@leedsmet.ac.uk
Tel: +44 113 812 4730
To view the terms under which this email is distributed, please go to http://disclaimer.leedsmet.ac.uk/email.htm