\$i++ problem understanding

Do you have a question? Post it now! No Registration Necessary.  Now with pictures!

•  Subject
• Author
• Posted on
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!

Re: \$i++ problem understanding

Pugi! wrote:

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

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:

Re: \$i++ problem understanding

Pugi! wrote:

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.

Re: \$i++ problem understanding

lain said the following on 05/10/2005 11:04:

Yup.

Nope :)

--
Oli

Re: \$i++ problem understanding

ouch, d'uh, i take that one back of course -.-

Re: \$i++ problem understanding

Pugi! wrote:
[snip]

[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

Re: \$i++ problem understanding

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

Re: \$i++ problem understanding

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

Re: \$i++ problem understanding

Hi,

What is the meaning of the | operator?

TIA

Re: \$i++ problem understanding

Michael Jack said the following on 11/10/2005 11:32:

Bitwise-OR.

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

--
Oli

Re: \$i++ problem understanding

Zilla said the following on 05/10/2005 13:39:

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

Re: \$i++ problem understanding

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

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@example.invalid
| http://www.newtnotes.com
DJMBS | http://newtnotes.com/doctor-jeff-master-brainsurgeon/

Re: \$i++ problem understanding

Pugi! (pugi@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.

Re: \$i++ problem understanding

Pugi! wrote:

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.

Re: \$i++ problem understanding

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

Re: \$i++ problem understanding

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.

Re: \$i++ problem understanding

Chung Leong wrote:

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@attglobal.net
==================

Re: \$i++ problem understanding

Jerry Stuckle wrote:

Or just make demons fly out of your nose.

Brian

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