logo       
Google Custom Search
    AddThis Social Bookmark Button
-->

Re: what happens with $b = $b++?: msg#01875

Subject: Re: what happens with $b = $b++?
re:"It seems that '$b = $b++;' is always a coding mistake. Nobody
should use that syntax on purpose"

True, True.  I can't see why you would ever choose to use that to get
$b to increment itself...just use "$b++" or "++$b" depending on the
effect you want!


I appreciate everyone who wrote, but I remain confused.

It seems to me that there is a weird short-circuit in the following code:

$b = 1;
$b = $b++;
print $b;

--> 1
I expected "2"

Whereas,

$b = 1
$b = ++$b;
print $b;

--> 2
As I expected.

I translate the autoincrement ($b++) as $b + 1, but post-increment
doesn't seem to work when assigned to itself, whereas pre-increment
does. So I was hoping for some confirmation that it is supposed to
work that way, and perhaps why...

So while Connie & Dave & Chris have answered the question to a
degree (and I thank them each), they haven't resolved my confusion.

It seems that '$b = $b++;' is always a coding mistake. Nobody should
use that syntax on purpose, apparently, because it ignores the 'side
effect' auto-increment. Is this true? I'll try to write my questions
better in the future.

/Michael Turner


--
Dave Tenen
basicSolutions?
59 Spec Pond Ave.
Lancaster, MA. 01523

Phone:(978) 537-6234
FAX:    (978) 537-2496
Cell:   (508) 873-8301
e-mail:dtenen@xxxxxxxxx

     "Let your solutions be our problems!"

Coming to you from Spec Pond, and I swear that fish
was [_______________________________________] that big!!!


<Prev in Thread] Current Thread [Next in Thread>