Connecting Tech Pros Worldwide Help | Site Map

$i = $i++

Steve
Guest
 
Posts: n/a
#1: Apr 11 '07
someone asked a question in alt.php about a problem they were having with an
algorytm. it contained something to the effect of:

$i = $i++;

some example code they'd snatched somewhere. in a loop, the expected $i to
increment. i explained why i thought it would not - as it does not. however,
i want to make sure i gave a valid answer.

anyone have input?


Jerry Stuckle
Guest
 
Posts: n/a
#2: Apr 11 '07

re: $i = $i++


Steve wrote:
Quote:
someone asked a question in alt.php about a problem they were having with an
algorytm. it contained something to the effect of:
>
$i = $i++;
>
some example code they'd snatched somewhere. in a loop, the expected $i to
increment. i explained why i thought it would not - as it does not. however,
i want to make sure i gave a valid answer.
>
anyone have input?
>
>
$i is incremented. Then $i is set with the old (pre-increment) value of
$i (++ has precedence over =).



--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex@attglobal.net
==================
Andy Hassall
Guest
 
Posts: n/a
#3: Apr 11 '07

re: $i = $i++


On Wed, 11 Apr 2007 16:20:45 -0500, "Steve" <no.one@example.comwrote:
Quote:
>someone asked a question in alt.php about a problem they were having with an
>algorytm. it contained something to the effect of:
>
>$i = $i++;
>
>some example code they'd snatched somewhere. in a loop, the expected $i to
>increment. i explained why i thought it would not - as it does not. however,
>i want to make sure i gave a valid answer.
>
>anyone have input?
In C and C++ that's undefined. PHP is less formally defined and so it is even
more daft to try it.

http://c-faq.com/expr/seqpoints.html
http://c-faq.com/expr/ieqiplusplus.html
Anonymous
Guest
 
Posts: n/a
#4: Apr 11 '07

re: $i = $i++


Steve wrote:
Quote:
>
someone asked a question in alt.php about a problem they were having with an
algorytm. it contained something to the effect of:
>
$i = $i++;
>
some example code they'd snatched somewhere. in a loop, the expected $i to
increment. i explained why i thought it would not - as it does not. however,
i want to make sure i gave a valid answer.
>
anyone have input?
I don't read alt.php so I don't know what you wrote, but the answer is
pretty obvious.

First, the ++ operators are increment operators. They execute an
incrementation of the variable by 1. $i++ by itself already executes $i
= $i + 1, which the original poster most likely wanted to do. But the
increment operators also return a value for further usage. The
post-increment operator returns the value before the variable was
incremented, the pre-increment operator returns the value after
incrementation.

$i++ is post-increment. That means, first the content of the variable is
evaluated, then it is incremented, then the evaluated (meaning the
former) value is passed to the equation which gets executed and resets
$i to its former value.

In other words, what the line $i=$i++; actually does is:

$evaluated = $i; //first save the current value for later usage
$i = $i + 1; // execute the increment after that because we are doing
post-increment
$i = $evaluated; // execute the actual equation

So I'd be very surprised if $i would change. :-)

Imagine the code:

$a = 1;
$b = $a++;

Then $a would be 2 and $b would be 1. If you wanted $b to get the new
value of $a and not the old one you would have to use pre-increment. The
variable would first be incremented and the new value passed to the
equation. Pre-increment would be ++$a instead of $a++. And if you wanted
$b to be just 1 larger than $a without changing $a at all you would
write $b = $a + 1;.

Pretty simple once you understand the concept of the increment (or
decrement) operators. If you (respectively the original posters) are
still unsure about them you should read them up in chapter 14 und 15 of
the PHP manual. They give several examples for using them. Feel free to
forward my explanation to alt.php, Steve.

Bye!
Tyno Gendo
Guest
 
Posts: n/a
#5: Apr 11 '07

re: $i = $i++


Andy Hassall wrote:
Quote:
On Wed, 11 Apr 2007 16:20:45 -0500, "Steve" <no.one@example.comwrote:
>
Quote:
>someone asked a question in alt.php about a problem they were having with an
>algorytm. it contained something to the effect of:
>>
>$i = $i++;
>>
>some example code they'd snatched somewhere. in a loop, the expected $i to
>increment. i explained why i thought it would not - as it does not. however,
>i want to make sure i gave a valid answer.
>>
>anyone have input?
>
In C and C++ that's undefined. PHP is less formally defined and so it is even
more daft to try it.
>
http://c-faq.com/expr/seqpoints.html
http://c-faq.com/expr/ieqiplusplus.html
Yes, definately a syntax to avoid, seems unpredictable at best...

<?php
$i = 2;
echo $i++ + 1; /// i'd expect 4, gives 3, ignores ++ increment

$i = 2;
echo $i++ + $i; // expect 5, gives 5, applies ++ increment

$i = 2;
echo $i++ + $i++ + 1; // expect 7, get 6
?>
Tyno Gendo
Guest
 
Posts: n/a
#6: Apr 11 '07

re: $i = $i++


Anonymous wrote:
Quote:
Steve wrote:
Quote:
>someone asked a question in alt.php about a problem they were having with an
>algorytm. it contained something to the effect of:
>>
>$i = $i++;
>>
>some example code they'd snatched somewhere. in a loop, the expected $i to
>increment. i explained why i thought it would not - as it does not. however,
>i want to make sure i gave a valid answer.
>>
>anyone have input?
>
I don't read alt.php so I don't know what you wrote, but the answer is
pretty obvious.
>
First, the ++ operators are increment operators. They execute an
incrementation of the variable by 1. $i++ by itself already executes $i
= $i + 1, which the original poster most likely wanted to do. But the
increment operators also return a value for further usage. The
post-increment operator returns the value before the variable was
incremented, the pre-increment operator returns the value after
incrementation.
>
$i++ is post-increment. That means, first the content of the variable is
evaluated, then it is incremented, then the evaluated (meaning the
former) value is passed to the equation which gets executed and resets
$i to its former value.
>
In other words, what the line $i=$i++; actually does is:
>
$evaluated = $i; //first save the current value for later usage
$i = $i + 1; // execute the increment after that because we are doing
post-increment
$i = $evaluated; // execute the actual equation
>
So I'd be very surprised if $i would change. :-)
>
Imagine the code:
>
$a = 1;
$b = $a++;
>
Then $a would be 2 and $b would be 1. If you wanted $b to get the new
value of $a and not the old one you would have to use pre-increment. The
variable would first be incremented and the new value passed to the
equation. Pre-increment would be ++$a instead of $a++. And if you wanted
$b to be just 1 larger than $a without changing $a at all you would
write $b = $a + 1;.
>
Pretty simple once you understand the concept of the increment (or
decrement) operators. If you (respectively the original posters) are
still unsure about them you should read them up in chapter 14 und 15 of
the PHP manual. They give several examples for using them. Feel free to
forward my explanation to alt.php, Steve.
>
Bye!
Ah, right, I see.. so from my previous post...

$i = 2;
echo $i++ + $i; // expect 5, gives 5, applies ++ increment

i complete messed up on that one...

but seeing your explanation i can see this gives

2 (return value of $i++) + 3 (now incremented value) = 5

it all makes sense, in a non-sensical fashion, avoid this syntax :)
Gordon Burditt
Guest
 
Posts: n/a
#7: Apr 11 '07

re: $i = $i++


>Yes, definately a syntax to avoid, seems unpredictable at best...
Quote:
>
><?php
$i = 2;
echo $i++ + 1; /// i'd expect 4, gives 3, ignores ++ increment
Please get your expecter fixed. $i++ returns the value *BEFORE*
the increment. (If you want ++$i, you know where to find it.) This
one is unambiguous since all possible orders come up with the same
result.

For the other expressions involving $i++ and another reference to $i's
value to give a resource returning Donald Trump, who's going to find the
programmer and say "You're Fired".

Anonymous
Guest
 
Posts: n/a
#8: Apr 12 '07

re: $i = $i++


Yes, definately a syntax to avoid, seems unpredictable at best...

No, I would always expect what you got, it's perfectly predictable. You
should read up how the ++ operators work.
Quote:
>
<?php
$i = 2;
echo $i++ + 1; /// i'd expect 4, gives 3, ignores ++ increment
Does not ignore the increment, but $i++ returns the value before
incrementation, then increments. So $i++ + 1 correctly returns 2 + 1,
which is 3.
Quote:
$i = 2;
echo $i++ + $i; // expect 5, gives 5, applies ++ increment
Correct. First $i++ is executed which returns 2, then increments a to 3,
then the + $i will be evaluated, so the expression will be 2 + 3, which
is 5.
Quote:
$i = 2;
echo $i++ + $i++ + 1; // expect 7, get 6
?>
Why do you expect 7? First $i++ returns 2, increments $i to 3, so the
expression will be 2 + $i++ + 1, with $i = 3, the second $i++ will
return 3 and increment $i to 4, so we have 2 + 3 + 1, which equals 6.

Bye!
Tyno Gendo
Guest
 
Posts: n/a
#9: Apr 12 '07

re: $i = $i++


Gordon Burditt wrote:
Quote:
Quote:
>Yes, definately a syntax to avoid, seems unpredictable at best...
>>
><?php
> $i = 2;
> echo $i++ + 1; /// i'd expect 4, gives 3, ignores ++ increment
>
Please get your expecter fixed. $i++ returns the value *BEFORE*
the increment. (If you want ++$i, you know where to find it.) This
one is unambiguous since all possible orders come up with the same
result.
>
For the other expressions involving $i++ and another reference to $i's
value to give a resource returning Donald Trump, who's going to find the
programmer and say "You're Fired".
>
Yes i can see that, and I'd ever use this crazy syntax anyway, but the
"human" way of reading this would seem to make it make sense so you can
understand how easy it is to be misinterpreted.

to the average bod....

$i++ would say 2+1 which is 3 (shock), yes... + 1, yep, 4, ERM... NO.

crazy computers, they do the most crazy things ;)

Tyno Gendo
Guest
 
Posts: n/a
#10: Apr 12 '07

re: $i = $i++


Anonymous wrote:
Quote:
Quote:
>Yes, definately a syntax to avoid, seems unpredictable at best...
>
No, I would always expect what you got, it's perfectly predictable. You
should read up how the ++ operators work.
>
Quote:
><?php
> $i = 2;
> echo $i++ + 1; /// i'd expect 4, gives 3, ignores ++ increment
>
Does not ignore the increment, but $i++ returns the value before
incrementation, then increments. So $i++ + 1 correctly returns 2 + 1,
which is 3.
>
Quote:
> $i = 2;
> echo $i++ + $i; // expect 5, gives 5, applies ++ increment
>
Correct. First $i++ is executed which returns 2, then increments a to 3,
then the + $i will be evaluated, so the expression will be 2 + 3, which
is 5.
>
Quote:
> $i = 2;
> echo $i++ + $i++ + 1; // expect 7, get 6
>?>
>
Why do you expect 7? First $i++ returns 2, increments $i to 3, so the
expression will be 2 + $i++ + 1, with $i = 3, the second $i++ will
return 3 and increment $i to 4, so we have 2 + 3 + 1, which equals 6.
>
Bye!
I have now, and it makes sense...

I'm glad this thread came up on alt.php as I'd never even considered
doing the $i = $i++ before someone posted it on there, but now I've seen
someone do it then I won't make the same mistake they did, and I didn't
realise that a '++' first returned the original value as if it was a
function...

But clearly, the PHP man says it does:

"$a++ Post-increment Returns $a, then increments $a by one."
Jerry Stuckle
Guest
 
Posts: n/a
#11: Apr 12 '07

re: $i = $i++


Gordon Burditt wrote:
Quote:
Quote:
>Yes, definately a syntax to avoid, seems unpredictable at best...
>>
><?php
> $i = 2;
> echo $i++ + 1; /// i'd expect 4, gives 3, ignores ++ increment
>
Please get your expecter fixed. $i++ returns the value *BEFORE*
the increment. (If you want ++$i, you know where to find it.) This
one is unambiguous since all possible orders come up with the same
result.
>
For the other expressions involving $i++ and another reference to $i's
value to give a resource returning Donald Trump, who's going to find the
programmer and say "You're Fired".
>
Actually, not entirely true.

<?
$i = 2;
echo $i++ + $i++ + 1; // expect 7, get 6
?>

can return either 5 or 6.

The reason here is - the order of operations is guaranteed, but not the
order the *operands* are evaluated.

For instance - the first $i++ is incremented to 3 and the old value (2)
returned. But there is no guarantee the value of the second $i++ will
be evaluated before or after the first $i++ has been processed.

This might be:

2 + 3 + 1

but

2 + 2 + 1

could also be valid, as could

3 + 2 + 1

Or course the first and third return the same value - but only because
it's addition.

In either case, however, $i will be incremented to 4 at the end.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex@attglobal.net
==================
Jerry Stuckle
Guest
 
Posts: n/a
#12: Apr 12 '07

re: $i = $i++


Andy Hassall wrote:
Quote:
On Wed, 11 Apr 2007 16:20:45 -0500, "Steve" <no.one@example.comwrote:
>
Quote:
>someone asked a question in alt.php about a problem they were having with an
>algorytm. it contained something to the effect of:
>>
>$i = $i++;
>>
>some example code they'd snatched somewhere. in a loop, the expected $i to
>increment. i explained why i thought it would not - as it does not. however,
>i want to make sure i gave a valid answer.
>>
>anyone have input?
>
In C and C++ that's undefined. PHP is less formally defined and so it is even
more daft to try it.
>
http://c-faq.com/expr/seqpoints.html
http://c-faq.com/expr/ieqiplusplus.html
Andy,

Actually, in both C and C++ this special case is defined. However, a
number of other cases aren't - i.e.

$i = 2;
func($i++, $i++);

could pass (2, 3), (3, 2) or even (2, 2) - depending on how the operands
are processed.

Remember - operators have precedence, but this does not generally apply
to operands. Assignment is a special case as it is always processed
last (other than the comma operator).


--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex@attglobal.net
==================
Steve
Guest
 
Posts: n/a
#13: Apr 12 '07

re: $i = $i++


On Wed, 11 Apr 2007 22:42:24 +0000, Gordon Burditt wrote:
Quote:
For the other expressions involving $i++ and another reference to $i's
value to give a resource returning Donald Trump, who's going to find the
programmer and say "You're Fired".
....is the correct answer!
Toby A Inkster
Guest
 
Posts: n/a
#14: Apr 12 '07

re: $i = $i++


Tyno Gendo wrote:
Quote:
I didn't realise that a '++' first returned the original value as if it
was a function...
Post-increment vs pre-increment:

++$i increments then returns value
$i++ returns value then increments

--
Toby A Inkster BSc (Hons) ARCS
Contact Me ~ http://tobyinkster.co.uk/contact
Geek of ~ HTML/SQL/Perl/PHP/Python*/Apache/Linux

* = I'm getting there!
Tyno Gendo
Guest
 
Posts: n/a
#15: Apr 12 '07

re: $i = $i++


Steve wrote:
Quote:
On Wed, 11 Apr 2007 22:42:24 +0000, Gordon Burditt wrote:
>
Quote:
>For the other expressions involving $i++ and another reference to $i's
>value to give a resource returning Donald Trump, who's going to find the
>programmer and say "You're Fired".
>
...is the correct answer!
I'll get my coat.

Vince Morgan
Guest
 
Posts: n/a
#16: Apr 12 '07

re: $i = $i++



"Tyno Gendo" <you@localhostwrote in message
news:tIGdneRI043Xd4DbnZ2dnUVZ8sfinZ2d@bt.com...
Quote:
Steve wrote:
Quote:
On Wed, 11 Apr 2007 22:42:24 +0000, Gordon Burditt wrote:
Quote:
For the other expressions involving $i++ and another reference to $i's
value to give a resource returning Donald Trump, who's going to find
the
Quote:
Quote:
Quote:
programmer and say "You're Fired".
...is the correct answer!
I'll get my coat.
>
LOL
I like your attitude Tyno!!


David T. Ashley
Guest
 
Posts: n/a
#17: Apr 12 '07

re: $i = $i++


"Andy Hassall" <andy@andyh.co.ukwrote in message
news:63nq13tlvg0fvp5i0lg54uvlo18nhi5n3f@4ax.com...
Quote:
On Wed, 11 Apr 2007 16:20:45 -0500, "Steve" <no.one@example.comwrote:
>
Quote:
>>someone asked a question in alt.php about a problem they were having with
>>an
>>algorytm. it contained something to the effect of:
>>
>>$i = $i++;
>>
>>some example code they'd snatched somewhere. in a loop, the expected $i to
>>increment. i explained why i thought it would not - as it does not.
>>however,
>>i want to make sure i gave a valid answer.
>>
>>anyone have input?
>
In C and C++ that's undefined. PHP is less formally defined and so it is
even
more daft to try it.
>
http://c-faq.com/expr/seqpoints.html
http://c-faq.com/expr/ieqiplusplus.html
I've never really understood why

$i = $i++;

_needs_ to be undefined in C, C++, or PHP. It seems that in order to assign
the left side, one needs to evaluate the right side, and since ++ has to
bind tighter than = ... it seems there should be an unequivocal "right"
answer ... $i will be unchanged.

Nonetheless, I do agree that it IS undefined, i.e.

http://c-faq.com/expr/ieqiplusplus.html

When I think of undefined, I think of something like:

my_function($i++, $i, $i--);

where for example the C language standard says that the order of evaluation
of function arguments is undefined.
--
David T. Ashley (dta@e3ft.com)
http://www.e3ft.com (Consulting Home Page)
http://www.dtashley.com (Personal Home Page)
http://gpl.e3ft.com (GPL Publications and Projects)


Steve
Guest
 
Posts: n/a
#18: Apr 12 '07

re: $i = $i++



"Steve" <no.one@example.comwrote in message
news:JgcTh.1016$AE5.731@newsfe06.lga...
| someone asked a question in alt.php about a problem they were having with
an
| algorytm. it contained something to the effect of:
|
| $i = $i++;
|
| some example code they'd snatched somewhere. in a loop, the expected $i to
| increment. i explained why i thought it would not - as it does not.
however,
| i want to make sure i gave a valid answer.
|
| anyone have input?


thanks all. that's how i explained it. i didn't quite get why the value of
$i remained the same (whatever it's initial value was)...until:

$i is incremented. Then $i is set with the old (pre-increment) value of
$i (++ has precedence over =).

thanks jerry. it just snapped for me. it's that even though $i++ may
increment, the lhs assignment is made from $i's initial value prior to the
increment... it's the = that resets/nullifies the ++ in this case. that
about right?


Rami Elomaa
Guest
 
Posts: n/a
#19: Apr 12 '07

re: $i = $i++


Steve kirjoitti:
Quote:
"Steve" <no.one@example.comwrote in message
news:JgcTh.1016$AE5.731@newsfe06.lga...
| someone asked a question in alt.php about a problem they were having with
an
| algorytm. it contained something to the effect of:
|
| $i = $i++;
|
| some example code they'd snatched somewhere. in a loop, the expected $i to
| increment. i explained why i thought it would not - as it does not.
however,
| i want to make sure i gave a valid answer.
|
| anyone have input?
>
>
thanks all. that's how i explained it. i didn't quite get why the value of
$i remained the same (whatever it's initial value was)...until:
>
$i is incremented. Then $i is set with the old (pre-increment) value of
$i (++ has precedence over =).
>
thanks jerry. it just snapped for me. it's that even though $i++ may
increment, the lhs assignment is made from $i's initial value prior to the
increment... it's the = that resets/nullifies the ++ in this case. that
about right?
Yes. That's why the assignment is not used with $i++. $i++ is a
shorthand that both increments and assigns at the same time. If you go
and add another assignment then it's fucked.

$i = ++$i; // this would've worked as expected, though.

Maybe this helps:

<?php

function ipp(&$i){ // equivalent of $i++
$old_i = $i; // old value is stored in temporary location
$i=$i+1; // value is altered
return $old_i; // old value is returned
}

$i = 5;
$i = ipp($i);
echo $i;

function ppi(&$i){ // equivalent of ++$i
$i=$i+1; // value is altered
return $i; // new value is returned
}

$j = 10;
$j = ppi($j);
echo $j;

?>

--
Rami.Elomaa@gmail.com

"Wikipedia on vähän niinq internetin raamattu, kukaan ei pohjimmiltaan
usko siihen ja kukaan ei tiedä mikä pitää paikkansa." -- z00ze
Steve
Guest
 
Posts: n/a
#20: Apr 12 '07

re: $i = $i++


| thanks all. that's how i explained it. i didn't quite get why the value
of
| $i remained the same (whatever it's initial value was)...until:
| >
| $i is incremented. Then $i is set with the old (pre-increment) value of
| $i (++ has precedence over =).
| >
| thanks jerry. it just snapped for me. it's that even though $i++ may
| increment, the lhs assignment is made from $i's initial value prior to
the
| increment... it's the = that resets/nullifies the ++ in this case. that
| about right?
|
| Yes. That's why the assignment is not used with $i++. $i++ is a
| shorthand that both increments and assigns at the same time. If you go
| and add another assignment then it's fucked.
|
| $i = ++$i; // this would've worked as expected, though.
|
| Maybe this helps:

no, no...i got it. not only did i explain it correctly in alt.php but gave
examples - even explaining that $i = ++$i *would* give expected results. i
was just trying to do it all in my head and match it up with each example's
results. i'm there now.

thx,

me


Malcolm Dew-Jones
Guest
 
Posts: n/a
#21: Apr 12 '07

re: $i = $i++


David T. Ashley (dta@e3ft.com) wrote:
: "Andy Hassall" <andy@andyh.co.ukwrote in message
: news:63nq13tlvg0fvp5i0lg54uvlo18nhi5n3f@4ax.com...
: On Wed, 11 Apr 2007 16:20:45 -0500, "Steve" <no.one@example.comwrote:
: >
: >>someone asked a question in alt.php about a problem they were having with
: >>an
: >>algorytm. it contained something to the effect of:
: >>
: >>$i = $i++;
: >>
: >>some example code they'd snatched somewhere. in a loop, the expected $i to
: >>increment. i explained why i thought it would not - as it does not.
: >>however,
: >>i want to make sure i gave a valid answer.
: >>
: >>anyone have input?
: >
: In C and C++ that's undefined. PHP is less formally defined and so it is
: even
: more daft to try it.
: >
: http://c-faq.com/expr/seqpoints.html
: http://c-faq.com/expr/ieqiplusplus.html

: I've never really understood why

: $i = $i++;

: _needs_ to be undefined in C, C++, or PHP. It seems that in order to assign
: the left side, one needs to evaluate the right side,

No, in order to assign the left side you only need to evaluate that part
of the right side that provides the original (pre-incremented) value.
The increment can happen any time afterwards as long as it's before the
next occasion when the variable is used. In fact if the variable was not
used later then the compiler could skip the increment altogether.

The language definition of C does not restrict the ordering so that
compilers are free to use as many techniques as possible to optimize the
code. Operations such as an increment can be run in parallel with the
rest of a statement (on hardware that has that sort of capability), but
only if the value does not need to be available within the statement
itself.

However, I don't write compilers so what I say is just my interpretation
of other things I've read.
Gordon Burditt
Guest
 
Posts: n/a
#22: Apr 12 '07

re: $i = $i++


>I've never really understood why
Quote:
>
$i = $i++;
>
>_needs_ to be undefined in C, C++, or PHP.
It's difficult to find these things at compile time, or it would
have been defined as a fatal error at compile time. $i = $i++ is
a simple and obvious case of a much more complicated problem.
Preferably a standard would also demand deletion of all copies of
the source code the compiler could find.
Quote:
>It seems that in order to assign
>the left side, one needs to evaluate the right side, and since ++ has to
>bind tighter than = ... it seems there should be an unequivocal "right"
>answer ... $i will be unchanged.
Which binds tighter, the increment of $i or returning the value of the
previous value? That's two separate operations represented by ONE
operator, ++.

Closed Thread