Re: RES: [PHP] inexplicable behaviour SOLVED
| From: | PJ | Date: | Tue, 28 Apr 2009 10:32:42 +0000 |
| Subject: | Re: RES: [PHP] inexplicable behaviour SOLVED | ||
| References: | 1 2 3 4 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-292064@lists.php.net to get a copy of this message | ||
Richard Quadling wrote:
> 2009/4/27 9el <lenin@phpxperts.net>:
>
>>> 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?
>>>
>>>
>
> $Count1 = $Count++;
>
> is not the same as
>
> $Count1 = $Count + 1;
>
>
> <?php
> $Count1 = 100;
> echo "Start with $Count1", PHP_EOL;
> $Count1 = $Count1++;
> echo "For \$Count1 = \$Count1++; the value in \$Count1 is $Count1", PHP_EOL;
> $Count1 = $Count1 + 1;
> echo "For \$Count1 = \$Count1 + 1; the value in \$Count1 is $Count1", PHP_EOL;
>
>
> outputs ...
>
> Start with 100
> For $Count1 = $Count1++; the value in $Count1 is 100
> For $Count1 = $Count1 + 1; the value in $Count1 is 101
>
>
> This shows that post-inc during an assignment does not affect the
> value assigned.
>
>
> Something that I thought would happen was if I ...
>
> <?php
> $Count1 = 100;
> echo $Count1 = $Count1++, PHP_EOL;
> echo $Count1, PHP_EOL;
>
> I thought I'd get ...
>
> 101
> 100
>
> but I get
>
> 100
> 100
>
> I thought the ++ would happen AFTER the assignment and the ++ to the
> value of the assignment. But this is not the case.
>
>
>> $Count = $Count + 1; is exactly(?) same as $Count++; Â or ++$Count
>> But not exactly same. Â PostFix notation adds the value after assigning.
>> PreFix notation adds the value right away.
>> But optimized programming argues about how machine is coded nowadays.
>>
>>
>>
>>> Anyway, I don't need that statement anymore as I found the error of my
>>> ways and have corrected it. And behold, the light came forth and it
>>> worked. :-)
>>>
>>>
>> Regards
>>
>> Lenin
>>
>> www.twitter.com/nine_L
>> www.lenin9l.wordpress.com
>>
Thanks. That is really a nice eye-opener.
Phil
--
Hervé Kempf: "Pour sauver la planète, sortez du capitalisme."
-------------------------------------------------------------
Phil Jourdan --- pj@ptahhotep.com
http://www.ptahhotep.com
http://www.chiccantine.com/andypantry.php