$i++ problem understanding

Currently I am studying PHP.

I can understand the following :
$a = 10;
$b = $a++;
print("$a, $b");
will print 11, 10 on the screen.
because when $b = $a++ first thing that happens is $b = $a
and then $a = $a + 1. I am willing to and can accept that.

But much harder to accept is and I fail to see the logic in it,
is the following :
$i = 1;
print($i++);
This will print 1 and only afterward will the value of $i be increased
by 1. It is between (), so logic tells me first $i++ and then print.

Can anyone help me understand this or see the logic in it ?


thanx,

Pugi!
pugi [ Mi, 05 Oktober 2005 11:26 ] [ ID #998324 ]

Re: $i++ problem understanding

Pugi! wrote:
> Currently I am studying PHP.
>
> I can understand the following :
> $a = 10;
> $b = $a++;
> print("$a, $b");
> will print 11, 10 on the screen.
> because when $b = $a++ first thing that happens is $b = $a
> and then $a = $a + 1. I am willing to and can accept that.
>
> But much harder to accept is and I fail to see the logic in it,
> is the following :
> $i = 1;
> print($i++);

its the same as in $b=$a++; the ++ comes last.

> This will print 1 and only afterward will the value of $i be increased
> by 1. It is between (), so logic tells me first $i++ and then print.
>
> Can anyone help me understand this or see the logic in it ?
>
>
> thanx,
>
> Pugi!
Stefan Rybacki [ Mi, 05 Oktober 2005 11:33 ] [ ID #998325 ]

Re: $i++ problem understanding

$a++
always means: use the OLD value and AFTER that add one
in some other languages there is the ++a expression which means add
first the use the result; don't know if that can be done in php
Greets
Thomas

--



Pugi! wrote:

> Currently I am studying PHP.
>
> I can understand the following :
> $a = 10;
> $b = $a++;
> print("$a, $b");
> will print 11, 10 on the screen.
> because when $b = $a++ first thing that happens is $b = $a
> and then $a = $a + 1. I am willing to and can accept that.
>
> But much harder to accept is and I fail to see the logic in it,
> is the following :
> $i = 1;
> print($i++);
> This will print 1 and only afterward will the value of $i be
> increased by 1. It is between (), so logic tells me first $i++ and
> then print.
>
> Can anyone help me understand this or see the logic in it ?
>
>
> thanx,
>
> Pugi!
Thomas Venus [ Mi, 05 Oktober 2005 11:48 ] [ ID #998326 ]

Re: $i++ problem understanding

Pugi! wrote:
> Currently I am studying PHP.
>
> I can understand the following :
> $a = 10;
> $b = $a++;
> print("$a, $b");
> will print 11, 10 on the screen.

just wrong.
what happens is:
$b = $a;
$a++;

so the above example actually will print ("10, 10"). understanding this
will also help you understanding the other example of yours. $a++ is
called "post increment" which means incrementing AFTER using the value
of this. of course there is also "pre increment" as of ++$a which will
do the opposite: incrementing the value and then use it for the
expression.
lain [ Mi, 05 Oktober 2005 12:04 ] [ ID #998328 ]

Re: $i++ problem understanding

lain said the following on 05/10/2005 11:04:
> Pugi! wrote:
>
>>Currently I am studying PHP.
>>
>>I can understand the following :
>>$a = 10;
>>$b = $a++;
>>print("$a, $b");
>>will print 11, 10 on the screen.
>
>
> just wrong.
> what happens is:
> $b = $a;
> $a++;

Yup.

> so the above example actually will print ("10, 10").

Nope :)


--
Oli
Oli Filth [ Mi, 05 Oktober 2005 13:05 ] [ ID #998329 ]

Re: $i++ problem understanding

ouch, d'uh, i take that one back of course -.-
lain [ Mi, 05 Oktober 2005 14:35 ] [ ID #998331 ]

Re: $i++ problem understanding

On Wed, 05 Oct 2005 11:26:52 +0200, Pugi! wrote:

> Can anyone help me understand this or see the logic in it ?

I know others are explaining it already, but here is my go.

++ increments an integer variable by one.

Whenever ++ is tacked onto a variable, the variable is incremented by one.
It doesn't matter if the variable is being passed into a function, is
being assigned to someting else, is in a for() loop, or is by itself.

++ can come *before* or *after* the variable, like this:

$i++

or this:

++$i

If ++ comes *before* the variable, then the statement on that line FIRST
increments the variable, and then uses that incremented value in the rest
of the statement.

If ++ comes *after* the variable, then the statement on that line first
uses the CURRENT value of the variable, and then increments the variable
at the end of the statement.

For example:

{
$a = 1;
$b = 2;
$c = $a + $b++;
}

// $c will be set to 3 and $b will be incremented to 3


This is different from this example:

{
$a = 1;
$b = 2;
$c = $a + ++$b;
}

// $b is incremented to 3 and then $c is set to 4

To use ++ unambiguously, always put it on a line by itself:

{
$a = 1;
$b = 2;
$c = $a + $b;
$b++;
}

does the same thing as

{
$a = 1;
$b = 2;
$c = $a + $b;
++$b;
}

That example is much less ambiguous than either of the first two.

later...
--
JDS | jeffrey [at] example.invalid
| http://www.newtnotes.com
DJMBS | http://newtnotes.com/doctor-jeff-master-brainsurgeon/
jds [ Mi, 05 Oktober 2005 15:44 ] [ ID #998332 ]

Re: $i++ problem understanding

Pugi! (pugi [at] group.thnx) wrote:
: Currently I am studying PHP.

: I can understand the following :
: $a = 10;
: $b = $a++;
: print("$a, $b");
: will print 11, 10 on the screen.
: because when $b = $a++ first thing that happens is $b = $a
: and then $a = $a + 1. I am willing to and can accept that.

: But much harder to accept is and I fail to see the logic in it,
: is the following :
: $i = 1;
: print($i++);
: This will print 1 and only afterward will the value of $i be increased
: by 1. It is between (), so logic tells me first $i++ and then print.

: Can anyone help me understand this or see the logic in it ?

The logic in it is simply that the post-increment operator ($variable++)
is supposed to work that way. That's what it is used for, incrementing
the value _after_ you use it.


If you want the variable to be incremented _before_ the value is printed
then you could use the pre-increment operator (++$variable) instead.


$i=10;
print ++$i;

# the above prints 11, just like you wanted.


--

This programmer available for rent.
yf110 [ Mi, 05 Oktober 2005 21:37 ] [ ID #998342 ]

Re: $i++ problem understanding

Pugi! wrote:
> Currently I am studying PHP.
>
> I can understand the following :
> $a = 10;
> $b = $a++;
> print("$a, $b");
> will print 11, 10 on the screen.
> because when $b = $a++ first thing that happens is $b = $a
> and then $a = $a + 1. I am willing to and can accept that.
>
> But much harder to accept is and I fail to see the logic in it,
> is the following :
> $i = 1;
> print($i++);

its the same as in $b=$a++; the ++ comes last.

> This will print 1 and only afterward will the value of $i be increased
> by 1. It is between (), so logic tells me first $i++ and then print.
>
> Can anyone help me understand this or see the logic in it ?
>
>
> thanx,
>
> Pugi!
Stefan Rybacki [ Mi, 05 Oktober 2005 11:33 ] [ ID #998387 ]

Re: $i++ problem understanding

$a++
always means: use the OLD value and AFTER that add one
in some other languages there is the ++a expression which means add
first the use the result; don't know if that can be done in php
Greets
Thomas

--



Pugi! wrote:

> Currently I am studying PHP.
>
> I can understand the following :
> $a = 10;
> $b = $a++;
> print("$a, $b");
> will print 11, 10 on the screen.
> because when $b = $a++ first thing that happens is $b = $a
> and then $a = $a + 1. I am willing to and can accept that.
>
> But much harder to accept is and I fail to see the logic in it,
> is the following :
> $i = 1;
> print($i++);
> This will print 1 and only afterward will the value of $i be
> increased by 1. It is between (), so logic tells me first $i++ and
> then print.
>
> Can anyone help me understand this or see the logic in it ?
>
>
> thanx,
>
> Pugi!
Thomas Venus [ Mi, 05 Oktober 2005 11:48 ] [ ID #998389 ]

Re: $i++ problem understanding

Pugi! wrote:
> Currently I am studying PHP.
>
> I can understand the following :
> $a = 10;
> $b = $a++;
> print("$a, $b");
> will print 11, 10 on the screen.

just wrong.
what happens is:
$b = $a;
$a++;

so the above example actually will print ("10, 10"). understanding this
will also help you understanding the other example of yours. $a++ is
called "post increment" which means incrementing AFTER using the value
of this. of course there is also "pre increment" as of ++$a which will
do the opposite: incrementing the value and then use it for the
expression.
lain [ Mi, 05 Oktober 2005 12:04 ] [ ID #998392 ]

Re: $i++ problem understanding

lain said the following on 05/10/2005 11:04:
> Pugi! wrote:
>
>>Currently I am studying PHP.
>>
>>I can understand the following :
>>$a = 10;
>>$b = $a++;
>>print("$a, $b");
>>will print 11, 10 on the screen.
>
>
> just wrong.
> what happens is:
> $b = $a;
> $a++;

Yup.

> so the above example actually will print ("10, 10").

Nope :)


--
Oli
Oli Filth [ Mi, 05 Oktober 2005 13:05 ] [ ID #998397 ]

Re: $i++ problem understanding

ouch, d'uh, i take that one back of course -.-
lain [ Mi, 05 Oktober 2005 14:35 ] [ ID #998399 ]

Re: $i++ problem understanding

Pugi! wrote:
[snip]
> But much harder to accept is and I fail to see the logic in it,
> is the following :
> $i = 1;
> print($i++);
> This will print 1 and only afterward will the value of $i be increased
> by 1. It is between (), so logic tells me first $i++ and then print.
[snip]

print is a language construct, not actually a function. That means that
the parentheses are optional. So the following two lines produces the
same result:

print "Hello World";
print("Hello World");

So in your example the parentheses don't have a meaning in the
mathematical sense of doing whats inside them first, which in php only
works when you are calculating something as in:

$number = (3 + 5) * 7;

where $number is 56 (8 * 7). That is why $i in your example is still
post-incremented, that is, after you have printed it.

Hope you understand me.
Zilla.

PS.: Btw, you can pre-increment with ++$i
Zilla [ Mi, 05 Oktober 2005 14:39 ] [ ID #998400 ]

Re: $i++ problem understanding

Zilla said the following on 05/10/2005 13:39:
> Pugi! wrote:
> [snip]
>
>> But much harder to accept is and I fail to see the logic in it,
>> is the following :
>> $i = 1;
>> print($i++);
>> This will print 1 and only afterward will the value of $i be increased
>> by 1. It is between (), so logic tells me first $i++ and then print.
>
> [snip]
>
> print is a language construct, not actually a function. That means that
> the parentheses are optional. So the following two lines produces the
> same result:
>
> print "Hello World";
> print("Hello World");
>
> So in your example the parentheses don't have a meaning in the
> mathematical sense of doing whats inside them first, which in php only
> works when you are calculating something as in:
>
> $number = (3 + 5) * 7;
>
> where $number is 56 (8 * 7). That is why $i in your example is still
> post-incremented, that is, after you have printed it.

The fact that print is a language construct (and therefore parantheses
are optional) is irrelevant. e.g.:

function foo($v)
{
echo $v;
}

$a = 5;
foo($a++);

Post-increment is always evaluated last.


--
Oli
Oli Filth [ Mi, 05 Oktober 2005 15:11 ] [ ID #998401 ]

Re: $i++ problem understanding

On Wed, 05 Oct 2005 11:26:52 +0200, Pugi! wrote:

> Can anyone help me understand this or see the logic in it ?

I know others are explaining it already, but here is my go.

++ increments an integer variable by one.

Whenever ++ is tacked onto a variable, the variable is incremented by one.
It doesn't matter if the variable is being passed into a function, is
being assigned to someting else, is in a for() loop, or is by itself.

++ can come *before* or *after* the variable, like this:

$i++

or this:

++$i

If ++ comes *before* the variable, then the statement on that line FIRST
increments the variable, and then uses that incremented value in the rest
of the statement.

If ++ comes *after* the variable, then the statement on that line first
uses the CURRENT value of the variable, and then increments the variable
at the end of the statement.

For example:

{
$a = 1;
$b = 2;
$c = $a + $b++;
}

// $c will be set to 3 and $b will be incremented to 3


This is different from this example:

{
$a = 1;
$b = 2;
$c = $a + ++$b;
}

// $b is incremented to 3 and then $c is set to 4

To use ++ unambiguously, always put it on a line by itself:

{
$a = 1;
$b = 2;
$c = $a + $b;
$b++;
}

does the same thing as

{
$a = 1;
$b = 2;
$c = $a + $b;
++$b;
}

That example is much less ambiguous than either of the first two.

later...
--
JDS | jeffrey [at] example.invalid
| http://www.newtnotes.com
DJMBS | http://newtnotes.com/doctor-jeff-master-brainsurgeon/
jds [ Mi, 05 Oktober 2005 15:44 ] [ ID #998402 ]

Re: $i++ problem understanding

Pugi! (pugi [at] group.thnx) wrote:
: Currently I am studying PHP.

: I can understand the following :
: $a = 10;
: $b = $a++;
: print("$a, $b");
: will print 11, 10 on the screen.
: because when $b = $a++ first thing that happens is $b = $a
: and then $a = $a + 1. I am willing to and can accept that.

: But much harder to accept is and I fail to see the logic in it,
: is the following :
: $i = 1;
: print($i++);
: This will print 1 and only afterward will the value of $i be increased
: by 1. It is between (), so logic tells me first $i++ and then print.

: Can anyone help me understand this or see the logic in it ?

The logic in it is simply that the post-increment operator ($variable++)
is supposed to work that way. That's what it is used for, incrementing
the value _after_ you use it.


If you want the variable to be incremented _before_ the value is printed
then you could use the pre-increment operator (++$variable) instead.


$i=10;
print ++$i;

# the above prints 11, just like you wanted.


--

This programmer available for rent.
yf110 [ Mi, 05 Oktober 2005 21:37 ] [ ID #998436 ]

Re: $i++ problem understanding

Pugi! wrote:
> Currently I am studying PHP.
>
> I can understand the following :
> $a = 10;
> $b = $a++;
> print("$a, $b");
> will print 11, 10 on the screen.
> because when $b = $a++ first thing that happens is $b = $a
> and then $a = $a + 1. I am willing to and can accept that.

I wouldn't try to understand the sublety of the post increment
operator. It really has no reason to exist other than the fact that its
syntax was modeled after that of C, a language that's much closer to
machine code. You wouldn't really see the logic behind it unless you
understand how a CPU works.

Post-increment means you use the value of a variable, then increment
it. This happens quite often in low-level programming. For example, to
compare two strings, you'd want to tell the CPU to compare the byte at
memory address A with the byte at memory address B, then increment A
and B, so that they point to the following pair of characters for the
next comparison. Instead sending three instructions--compare,
increment, increment--to a CPU like x86 you send just one--the "scan
string" instruction. This short-hand thus speed up the operation three
times.

In any event, the reason print($i++) yields 1 is because parentheses
don't actually do anything. They are only used for resolving
precedence. The post-increment occurs right after the variable is
read--after the print operation in this case.

Another example to consider is print ($i++ + $++). This'd be the
sequence of events:

1. To perform the addition operation, A + B, PHP reads $i (which is 1)
for A
2. PHP increment $i
3. PHP reads $i (which is now 2) for B
4. PHP increment $i again
5. PHP passes the result of A + B to print, which is 3

So you see, post increment isn't the last thing that happens. It
happens right after the variable is read.
Chung Leong [ Do, 06 Oktober 2005 04:12 ] [ ID #999791 ]

Re: $i++ problem understanding

"Chung Leong" <chernyshevsky [at] hotmail.com> wrote in message
news:1128564778.863249.81660 [at] g43g2000cwa.googlegroups.com...

> Another example to consider is print ($i++ + $++). This'd be the
> sequence of events:
>
> 1. To perform the addition operation, A + B, PHP reads $i (which is 1)
> for A
> 2. PHP increment $i
> 3. PHP reads $i (which is now 2) for B
> 4. PHP increment $i again
> 5. PHP passes the result of A + B to print, which is 3
>
> So you see, post increment isn't the last thing that happens. It
> happens right after the variable is read.

Actually, in C the result of the expression $i++ + $i++ is undefined.
Indeed, it produces different results on different computers.

The PHP manual seems to not discuss this point (OK, I only glanced at it
briefly, in the section on expressions).

But the PHP manual *does* say "just as in C" several times, which makes me
think that one ought to be cautious of writing things in PHP which are
undefined in C. In other words, even though it works OK today, it might not
work OK tomorrow.
Dana Cartwright [ Do, 06 Oktober 2005 14:04 ] [ ID #999800 ]

Re: $i++ problem understanding

>> I can understand the following :
>> $a = 10;
>> $b = $a++;
>> print("$a, $b");
>> will print 11, 10 on the screen.
>> because when $b = $a++ first thing that happens is $b = $a
>> and then $a = $a + 1. I am willing to and can accept that.
>
> I wouldn't try to understand the sublety of the post increment
> operator. It really has no reason to exist other than the fact that its
> syntax was modeled after that of C, a language that's much closer to
> machine code. You wouldn't really see the logic behind it unless you
> understand how a CPU works.
>
> Post-increment means you use the value of a variable, then increment
> it. This happens quite often in low-level programming. For example, to
> compare two strings, you'd want to tell the CPU to compare the byte at
> memory address A with the byte at memory address B, then increment A
> and B, so that they point to the following pair of characters for the
> next comparison. Instead sending three instructions--compare,
> increment, increment--to a CPU like x86 you send just one--the "scan
> string" instruction. This short-hand thus speed up the operation three
> times.
>
> In any event, the reason print($i++) yields 1 is because parentheses
> don't actually do anything. They are only used for resolving
> precedence. The post-increment occurs right after the variable is
> read--after the print operation in this case.
>
> Another example to consider is print ($i++ + $++). This'd be the
> sequence of events:
>
> 1. To perform the addition operation, A + B, PHP reads $i (which is 1)
> for A
> 2. PHP increment $i
> 3. PHP reads $i (which is now 2) for B
> 4. PHP increment $i again
> 5. PHP passes the result of A + B to print, which is 3
>
> So you see, post increment isn't the last thing that happens. It
> happens right after the variable is read.


Thanx for the clarification. I was wrong in my last post in that topic
and was unaware of it. I've read manuals for PHP (and earlier for C/C++)
describing it but for some cause I remembered it the wrong way and never
had to use it in a way you showed it (increment same variable twice
in same instruction), so I never hit the situation which would show me
I'm wrong. Thank you again.

Hilarion
Hilarion [ Do, 06 Oktober 2005 11:48 ] [ ID #999804 ]

Re: $i++ problem understanding

Maybe a little off topic, but I recall the good old days when a teacher
I had one day said she would give an extra point in the finals to the
first person to correctly say what would be the output of

$a = 1;
$b = 2;
$c = $a++ + ++$b;
echo "$a $b $c";

The fastest one to answer was, looking back now, not quite fast enough
:D
lets.monroe [ Do, 06 Oktober 2005 18:32 ] [ ID #999810 ]

Re: $i++ problem understanding

Chung Leong wrote:
> Pugi! wrote:
>
>>Currently I am studying PHP.
>>
>>I can understand the following :
>>$a = 10;
>>$b = $a++;
>>print("$a, $b");
>>will print 11, 10 on the screen.
>>because when $b = $a++ first thing that happens is $b = $a
>>and then $a = $a + 1. I am willing to and can accept that.
>
>
> I wouldn't try to understand the sublety of the post increment
> operator. It really has no reason to exist other than the fact that its
> syntax was modeled after that of C, a language that's much closer to
> machine code. You wouldn't really see the logic behind it unless you
> understand how a CPU works.
>
> Post-increment means you use the value of a variable, then increment
> it. This happens quite often in low-level programming. For example, to
> compare two strings, you'd want to tell the CPU to compare the byte at
> memory address A with the byte at memory address B, then increment A
> and B, so that they point to the following pair of characters for the
> next comparison. Instead sending three instructions--compare,
> increment, increment--to a CPU like x86 you send just one--the "scan
> string" instruction. This short-hand thus speed up the operation three
> times.
>
> In any event, the reason print($i++) yields 1 is because parentheses
> don't actually do anything. They are only used for resolving
> precedence. The post-increment occurs right after the variable is
> read--after the print operation in this case.
>
> Another example to consider is print ($i++ + $++). This'd be the
> sequence of events:
>
> 1. To perform the addition operation, A + B, PHP reads $i (which is 1)
> for A
> 2. PHP increment $i
> 3. PHP reads $i (which is now 2) for B
> 4. PHP increment $i again
> 5. PHP passes the result of A + B to print, which is 3
>
> So you see, post increment isn't the last thing that happens. It
> happens right after the variable is read.
>


Chung,

Actually, Dana is correct. "i++ + i++" is undefined in C. The language
defines the evaluation order of the operators - but not the operands.
So if i = 1, the result could be 1+1, 2+1 or 1+2 (although probably not
the last). It's up to the compiler manufacturers on how it's implemented.

Another example is func(i++, i++). This could be passed as (1,1), (1,2)
or (2,1) (again, probably not the last).

It's an example I've used quite frequently in my advanced C and C++ classes.

However - since PHP is basically written by one company and has pretty
much the same code base for all platforms, I would expect the same
version of PHP to act similarly across platforms. However, I wouldn't
want to guarantee it across versions.




--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex [at] attglobal.net
==================
Jerry Stuckle [ Do, 06 Oktober 2005 23:06 ] [ ID #999812 ]

Re: $i++ problem understanding

Pugi! wrote:
> Currently I am studying PHP.
>
> I can understand the following :
> $a = 10;
> $b = $a++;
> print("$a, $b");
> will print 11, 10 on the screen.
> because when $b = $a++ first thing that happens is $b = $a
> and then $a = $a + 1. I am willing to and can accept that.

I wouldn't try to understand the sublety of the post increment
operator. It really has no reason to exist other than the fact that its
syntax was modeled after that of C, a language that's much closer to
machine code. You wouldn't really see the logic behind it unless you
understand how a CPU works.

Post-increment means you use the value of a variable, then increment
it. This happens quite often in low-level programming. For example, to
compare two strings, you'd want to tell the CPU to compare the byte at
memory address A with the byte at memory address B, then increment A
and B, so that they point to the following pair of characters for the
next comparison. Instead sending three instructions--compare,
increment, increment--to a CPU like x86 you send just one--the "scan
string" instruction. This short-hand thus speed up the operation three
times.

In any event, the reason print($i++) yields 1 is because parentheses
don't actually do anything. They are only used for resolving
precedence. The post-increment occurs right after the variable is
read--after the print operation in this case.

Another example to consider is print ($i++ + $++). This'd be the
sequence of events:

1. To perform the addition operation, A + B, PHP reads $i (which is 1)
for A
2. PHP increment $i
3. PHP reads $i (which is now 2) for B
4. PHP increment $i again
5. PHP passes the result of A + B to print, which is 3

So you see, post increment isn't the last thing that happens. It
happens right after the variable is read.
Chung Leong [ Do, 06 Oktober 2005 04:12 ] [ ID #999830 ]

Re: $i++ problem understanding

"Chung Leong" <chernyshevsky [at] hotmail.com> wrote in message
news:1128564778.863249.81660 [at] g43g2000cwa.googlegroups.com...

> Another example to consider is print ($i++ + $++). This'd be the
> sequence of events:
>
> 1. To perform the addition operation, A + B, PHP reads $i (which is 1)
> for A
> 2. PHP increment $i
> 3. PHP reads $i (which is now 2) for B
> 4. PHP increment $i again
> 5. PHP passes the result of A + B to print, which is 3
>
> So you see, post increment isn't the last thing that happens. It
> happens right after the variable is read.

Actually, in C the result of the expression $i++ + $i++ is undefined.
Indeed, it produces different results on different computers.

The PHP manual seems to not discuss this point (OK, I only glanced at it
briefly, in the section on expressions).

But the PHP manual *does* say "just as in C" several times, which makes me
think that one ought to be cautious of writing things in PHP which are
undefined in C. In other words, even though it works OK today, it might not
work OK tomorrow.
Dana Cartwright [ Do, 06 Oktober 2005 14:04 ] [ ID #999852 ]

Re: $i++ problem understanding

>> But much harder to accept is and I fail to see the logic in it,
>> is the following :
>> $i = 1;
>> print($i++);
>> This will print 1 and only afterward will the value of $i be increased
>> by 1. It is between (), so logic tells me first $i++ and then print.
>> [snip]
>
> print is a language construct, not actually a function. That means that
> the parentheses are optional.
> [snip]

My two cents:
Post-increment operator does incrementation after the whole instruction
(which contains it) is executed, not after the expression containing it
is executed, so it does not matter if you are using some language construct
like "print" or "echo" or some function or expressions with different
operator priority and parentheses.

Example:

some_function(($i++ * 2) + 4) | 7);

is equivalent to:

some_function(($i * 2) + 4) | 7); $i += 1;


Example for pre-increment operator:

some_function((++$i * 2) + 4) | 7);

is equivalent to:

$i += 1; some_function(($i * 2) + 4) | 7);



Hilarion
Hilarion [ Mi, 05 Oktober 2005 15:07 ] [ ID #999857 ]

Re: $i++ problem understanding

>> I can understand the following :
>> $a = 10;
>> $b = $a++;
>> print("$a, $b");
>> will print 11, 10 on the screen.
>> because when $b = $a++ first thing that happens is $b = $a
>> and then $a = $a + 1. I am willing to and can accept that.
>
> I wouldn't try to understand the sublety of the post increment
> operator. It really has no reason to exist other than the fact that its
> syntax was modeled after that of C, a language that's much closer to
> machine code. You wouldn't really see the logic behind it unless you
> understand how a CPU works.
>
> Post-increment means you use the value of a variable, then increment
> it. This happens quite often in low-level programming. For example, to
> compare two strings, you'd want to tell the CPU to compare the byte at
> memory address A with the byte at memory address B, then increment A
> and B, so that they point to the following pair of characters for the
> next comparison. Instead sending three instructions--compare,
> increment, increment--to a CPU like x86 you send just one--the "scan
> string" instruction. This short-hand thus speed up the operation three
> times.
>
> In any event, the reason print($i++) yields 1 is because parentheses
> don't actually do anything. They are only used for resolving
> precedence. The post-increment occurs right after the variable is
> read--after the print operation in this case.
>
> Another example to consider is print ($i++ + $++). This'd be the
> sequence of events:
>
> 1. To perform the addition operation, A + B, PHP reads $i (which is 1)
> for A
> 2. PHP increment $i
> 3. PHP reads $i (which is now 2) for B
> 4. PHP increment $i again
> 5. PHP passes the result of A + B to print, which is 3
>
> So you see, post increment isn't the last thing that happens. It
> happens right after the variable is read.


Thanx for the clarification. I was wrong in my last post in that topic
and was unaware of it. I've read manuals for PHP (and earlier for C/C++)
describing it but for some cause I remembered it the wrong way and never
had to use it in a way you showed it (increment same variable twice
in same instruction), so I never hit the situation which would show me
I'm wrong. Thank you again.

Hilarion
Hilarion [ Do, 06 Oktober 2005 11:48 ] [ ID #999858 ]

Re: $i++ problem understanding

>>> But much harder to accept is and I fail to see the logic in it,
>>> is the following :
>>> $i = 1;
>>> print($i++);
>>> This will print 1 and only afterward will the value of $i be increased
>>> by 1. It is between (), so logic tells me first $i++ and then print.
>>> [snip]
>>
>> print is a language construct, not actually a function. That means that
>> the parentheses are optional.
>> [snip]
>
> My two cents:
> Post-increment operator does incrementation after the whole instruction
> (which contains it) is executed, not after the expression containing it
> is executed, so it does not matter if you are using some language construct
> like "print" or "echo" or some function or expressions with different
> operator priority and parentheses.
>
> Example:
>
> some_function(($i++ * 2) + 4) | 7);
>
> is equivalent to:
>
> some_function(($i * 2) + 4) | 7); $i += 1;
>
>
> Example for pre-increment operator:
>
> some_function((++$i * 2) + 4) | 7);
>
> is equivalent to:
>
> $i += 1; some_function(($i * 2) + 4) | 7);


I was wrong when I was writing it. If you are reading this post (possibly
in some newsgroup archives), then read the post by Chung Leong in this thread.
He described the operator correctly.


Hilarion
Hilarion [ Do, 06 Oktober 2005 12:00 ] [ ID #999859 ]

Re: $i++ problem understanding

Maybe a little off topic, but I recall the good old days when a teacher
I had one day said she would give an extra point in the finals to the
first person to correctly say what would be the output of

$a = 1;
$b = 2;
$c = $a++ + ++$b;
echo "$a $b $c";

The fastest one to answer was, looking back now, not quite fast enough
:D
lets.monroe [ Do, 06 Oktober 2005 18:32 ] [ ID #999874 ]

Re: $i++ problem understanding

Chung Leong wrote:
> Pugi! wrote:
>
>>Currently I am studying PHP.
>>
>>I can understand the following :
>>$a = 10;
>>$b = $a++;
>>print("$a, $b");
>>will print 11, 10 on the screen.
>>because when $b = $a++ first thing that happens is $b = $a
>>and then $a = $a + 1. I am willing to and can accept that.
>
>
> I wouldn't try to understand the sublety of the post increment
> operator. It really has no reason to exist other than the fact that its
> syntax was modeled after that of C, a language that's much closer to
> machine code. You wouldn't really see the logic behind it unless you
> understand how a CPU works.
>
> Post-increment means you use the value of a variable, then increment
> it. This happens quite often in low-level programming. For example, to
> compare two strings, you'd want to tell the CPU to compare the byte at
> memory address A with the byte at memory address B, then increment A
> and B, so that they point to the following pair of characters for the
> next comparison. Instead sending three instructions--compare,
> increment, increment--to a CPU like x86 you send just one--the "scan
> string" instruction. This short-hand thus speed up the operation three
> times.
>
> In any event, the reason print($i++) yields 1 is because parentheses
> don't actually do anything. They are only used for resolving
> precedence. The post-increment occurs right after the variable is
> read--after the print operation in this case.
>
> Another example to consider is print ($i++ + $++). This'd be the
> sequence of events:
>
> 1. To perform the addition operation, A + B, PHP reads $i (which is 1)
> for A
> 2. PHP increment $i
> 3. PHP reads $i (which is now 2) for B
> 4. PHP increment $i again
> 5. PHP passes the result of A + B to print, which is 3
>
> So you see, post increment isn't the last thing that happens. It
> happens right after the variable is read.
>


Chung,

Actually, Dana is correct. "i++ + i++" is undefined in C. The language
defines the evaluation order of the operators - but not the operands.
So if i = 1, the result could be 1+1, 2+1 or 1+2 (although probably not
the last). It's up to the compiler manufacturers on how it's implemented.

Another example is func(i++, i++). This could be passed as (1,1), (1,2)
or (2,1) (again, probably not the last).

It's an example I've used quite frequently in my advanced C and C++ classes.

However - since PHP is basically written by one company and has pretty
much the same code base for all platforms, I would expect the same
version of PHP to act similarly across platforms. However, I wouldn't
want to guarantee it across versions.




--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex [at] attglobal.net
==================
Jerry Stuckle [ Do, 06 Oktober 2005 23:06 ] [ ID #999898 ]

Re: $i++ problem understanding

Jerry Stuckle wrote:


> Actually, Dana is correct. "i++ + i++" is undefined in C. The
> language defines the evaluation order of the operators - but not the
> operands. So if i = 1, the result could be 1+1, 2+1 or 1+2 (although
> probably not the last). It's up to the compiler manufacturers on how
> it's implemented.

Or just make demons fly out of your nose.




Brian
defaultuserbr [ Fr, 07 Oktober 2005 20:40 ] [ ID #1001248 ]

Re: $i++ problem understanding

Jerry Stuckle wrote:


> Actually, Dana is correct. "i++ + i++" is undefined in C. The
> language defines the evaluation order of the operators - but not the
> operands. So if i = 1, the result could be 1+1, 2+1 or 1+2 (although
> probably not the last). It's up to the compiler manufacturers on how
> it's implemented.

Or just make demons fly out of your nose.




Brian
defaultuserbr [ Fr, 07 Oktober 2005 20:40 ] [ ID #1001293 ]

Re: $i++ problem understanding

Hi,

What is the meaning of the | operator?

TIA

"Hilarion" <hilarion [at] SPAM.op.SMIECI.pl> wrote in
> some_function(($i++ * 2) + 4) | 7);
>
> is equivalent to:
>
> some_function(($i * 2) + 4) | 7); $i += 1;
>
>
> Example for pre-increment operator:
>
> some_function((++$i * 2) + 4) | 7);
>
> is equivalent to:
>
> $i += 1; some_function(($i * 2) + 4) | 7);
>
>
>
> Hilarion
Michael Jack [ Di, 11 Oktober 2005 12:32 ] [ ID #1006215 ]

Re: $i++ problem understanding

Michael Jack said the following on 11/10/2005 11:32:
> Hi,
>
> What is the meaning of the | operator?
>

Bitwise-OR.

e.g. (1 | 2) == 3



--
Oli
catch [ Di, 11 Oktober 2005 13:26 ] [ ID #1006224 ]
PHP » alt.php » $i++ problem understanding

Vorheriges Thema: Passing data between php files
Nächstes Thema: phpMyChat Forum