You are here: Re: $i++ problem understanding « PHP Programming Language « IT news, forums, messages
Re: $i++ problem understanding

Posted by Malcolm Dew-Jones on 11/24/24 11:28

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.

 

Navigation:

[Reply to this message]


Удаленная работа для программистов  •  Как заработать на Google AdSense  •  England, UK  •  статьи на английском  •  PHP MySQL CMS Apache Oscommerce  •  Online Business Knowledge Base  •  DVD MP3 AVI MP4 players codecs conversion help
Home  •  Search  •  Site Map  •  Set as Homepage  •  Add to Favourites

Copyright © 2005-2006 Powered by Custom PHP Programming

Сайт изготовлен в Студии Валентина Петручека
изготовление и поддержка веб-сайтов, разработка программного обеспечения, поисковая оптимизация