473,386 Members | 1,766 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

$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!
Oct 5 '05 #1
19 2015
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!

Oct 5 '05 #2
$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!

Oct 5 '05 #3

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.

Oct 5 '05 #4
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
Oct 5 '05 #5
ouch, d'uh, i take that one back of course -.-

Oct 5 '05 #6
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
Oct 5 '05 #7
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
Oct 5 '05 #8
JDS
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 | je*****@example.invalid
| http://www.newtnotes.com
DJMBS | http://newtnotes.com/doctor-jeff-master-brainsurgeon/

Oct 5 '05 #9
Pugi! (pu**@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.
Oct 5 '05 #10
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.

Oct 6 '05 #11
"Chung Leong" <ch***********@hotmail.com> wrote in message
news:11*********************@g43g2000cwa.googlegro ups.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.
Oct 6 '05 #12
>> 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
Oct 6 '05 #13
>> 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
Oct 6 '05 #14
>>> 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
Oct 6 '05 #15
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

Oct 6 '05 #16
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.
js*******@attglobal.net
==================
Oct 6 '05 #17
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

Oct 7 '05 #18
Hi,

What is the meaning of the | operator?

TIA

"Hilarion" <hi******@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

Oct 11 '05 #19
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
Oct 11 '05 #20

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Mike | last post by:
Hey guys, need some help understanding some things that maybe someone can explain or clarify it a little better then a text book. Here is my understanding so far: Class - basically a shell for...
7
by: laura | last post by:
I'm trying to understand fully Server.MapPath. I am writing an intranet for a small company and want to be able to put some files accessible to all, hyperlinked from the intranet and therefore,...
26
by: Bail | last post by:
I will have a exam on the oncoming friday, my professor told us that it will base upon this program. i am having troubles understanding this program, for example what if i want to add all the...
11
by: Leon | last post by:
Are dataset automatically stored in memory? Does the dispose() method automatically dispose of the dataset in the code below? Do I have to dispose a dataset from memory or does the dataset...
18
by: Simon | last post by:
Hi, I understand what one the differences between std::vector, std::deque and std::list is, the std::vector can have data inserted/deleted at the end. The std::deque can have data...
4
by: joelagnel | last post by:
I'm new to .NET and have a basic understanding of operating systems. I can also do some magic in Csharp, but I lack an understanding of what's going on under the hood, with respect to how windows...
10
by: sasquatch | last post by:
X-No-Archive: Are there any good books that provide tips about how to understand and "play with" large C++ programs? It seems like a very important skill, and yet its hardly taught in the...
8
by: boki_pfc | last post by:
Hi Everybody, I am looking for an advice on following: I have that "pleasure" of reading C++ codes that have been written by person(s) that have not attended the same C++ classes that I did or...
2
by: subramanian100in | last post by:
Is my following understanding correct ? Data abstraction means providing the interface - that is, the set of functions that can be called by the user of a class. Information hiding means...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.