|
|||||||
New to SitePoint Forums? Register here for free!
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
SitePoint Wizard
![]() ![]() Join Date: Nov 2000
Location: Switzerland
Posts: 2,898
|
Trivia: incrementing $i
Here's a little piece of PHP trivia;
In PHP there are basically three main ways to increment a variable by one. Using addition your options are; PHP Code:
PHP Code:
PHP Code:
PHP Code:
Code:
$i=$i+1 Took: 4.1503800153732 $i++ Took: 2.8252899646759 ++$i Took: 2.7342989444733 Head here for more detail: http://www.php.net/manual/en/languag....increment.php OK - trivia over. Finally a brain teazer (don't give the game away by answering here); PHP Code:
|
|
|
|
|
|
#2 |
|
SitePoint Guru
![]() ![]() ![]() ![]() ![]() Join Date: Apr 2002
Location: Melbourne
Posts: 711
|
Pick me!! Pick me!!!
Very interesting about the $i++ and the ++$i I always use $i++ without thinking anything of it... ![]()
__________________
Websites: DH Softwares|Arcade Online|Bizarre Facts|Fun Arcade More sites: Gin Recipes|High Score Games|Cool Free Online Games Really Funny Clips at ReallyFunnyClips.com |
|
|
|
|
|
#3 |
|
32,817
![]() Join Date: Jun 2001
Location: Toronto, Canada
Posts: 9,292
|
++i is always faster in every programming language
![]() |
|
|
|
|
|
#4 |
|
FreeBSD The Power to Serve
![]() Join Date: Jul 2001
Location: Italy
Posts: 4,568
|
Harry,
you forget this case PHP Code:
Code:
$i=$i+1 Took: 4.01947200298 $i++ Took: 2.70495903492 ++$i Took: 2.622205019 $i += 1 Took: 2.75653505325 scoates did something similar if I'm right ![]() pippo |
|
|
|
|
|
#5 |
|
Forum Mathematics Geek
![]() ![]() ![]() Join Date: Aug 2002
Location: Commonwealth of Pennsylvania
Posts: 232
|
Did you know that the ++ operator works on numbers AND strings? a will turn to b, z will turn to aa. Pretty useful.
|
|
|
|
|
|
#6 |
|
Your Lord and Master, Foamy
![]() Join Date: Aug 1999
Location: Lancaster, Ca. USA
Posts: 12,363
|
Hmmm... Well no one answered the brain teaser.
I mostly use $i++, I would think most people do. It actually looks the most logical. However once in a while it is necessary to increment a variable after using it somewhere so ++$i comes in handy as well. |
|
|
|
|
|
#7 |
|
Forum Mathematics Geek
![]() ![]() ![]() Join Date: Aug 2002
Location: Commonwealth of Pennsylvania
Posts: 232
|
I'll make an educated guess and say 10.
Edit: ![]() |
|
|
|
|
|
#8 |
|
SitePoint Wizard
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Oct 2002
Location: Scotland
Posts: 3,107
|
My guess was wrong too
Back to elementary math for me I guess. ![]() |
|
|
|
|
|
#9 |
|
SitePoint Wizard
![]() ![]() Join Date: Nov 2000
Location: Switzerland
Posts: 2,898
|
It's a tricky one. And apparently if you try it with different programming languages, you'll get different answers!
|
|
|
|
|
|
#10 |
|
FreeBSD The Power to Serve
![]() Join Date: Jul 2001
Location: Italy
Posts: 4,568
|
>>> What will PHP echo at the end
6 *edited* I launched it...but I have to admit that I was wrong... ![]() *edited 2* In C the result is the same as in php: Code:
i = 1; i += ++i + i++; Last edited by pippo; Oct 2, 2002 at 07:06.. |
|
|
|
|
|
#11 | |
|
SitePoint Wizard
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Mar 2001
Location: London | UK
Posts: 1,140
|
Quote:
In case people don't understand the difference between $i++ and ++$i: ++$i is the pre-increment operator, and $i++ is the post-increment operator. To illustrate the difference, I'll use a simple example: PHP Code:
Code:
1 - 2 PHP Code:
Code:
2 - 2 I hope that's reasonably clear. ![]() Matt. ![]() |
|
|
|
|
|
|
#12 |
|
SitePoint Wizard
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Nov 2001
Location: Atlanta, GA, USA
Posts: 5,024
|
I found it very interesting that:
PHP Code:
PHP Code:
PHP Code:
PHP Code:
Does this basic theory hold up across most operations? (The fewer times you reference a variable, the faster it'll be.)
__________________
Using your unpaid time to add free content to SitePoint Pty Ltd's portfolio? |
|
|
|
|
|
#13 | |
|
SitePoint Wizard
![]() ![]() Join Date: Nov 2000
Location: Switzerland
Posts: 2,898
|
Quote:
I could be wrong in saying this but perhaps it's worth thinking about the = operator as meaning "creates new instance of". So $i = $i + 1; creates another instance of $i. As a matter of intest, that teazer in Java; PHP Code:
|
|
|
|
|
|
|
#14 |
|
SitePoint Evangelist
![]() ![]() ![]() ![]() Join Date: Nov 2001
Location: UK
Posts: 466
|
To go even faster (programming theory ergo untested) you could evaluate toward zero rather than toward 1000000.
Code:
$i = 1000000;
while($i>0) {
--$i;
}
Code:
$i = $variable;
while(--$i>0) {
//stuff
}
Subnote: I guessed 6 too ![]() Last edited by pootergeist; Oct 31, 2002 at 04:54.. |
|
|
|
|
|
#15 | |
|
morphine for a wooden leg
![]() ![]() ![]() ![]() ![]() Join Date: Jun 2002
Location: .chicago.il.us
Posts: 957
|
Quote:
++$i is a preincrement, which means increment the value in $i before evaluating it. $i++ is a postincrement, which means increment the value in $i after evaluating it. So, for instance Code:
$i = 1; print $i++;
__________________
----Adopt-a-Sig---- Your message here! |
|
|
|
|
|
|
#16 | |
|
SitePoint Wizard
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Mar 2001
Location: London | UK
Posts: 1,140
|
Quote:
![]() |
|
|
|
|
|
|
#17 | |
|
morphine for a wooden leg
![]() ![]() ![]() ![]() ![]() Join Date: Jun 2002
Location: .chicago.il.us
Posts: 957
|
Quote:
__________________
----Adopt-a-Sig---- Your message here! |
|
|
|
|
|
|
#18 |
|
will code HTML for food
![]() ![]() ![]() ![]() Join Date: Sep 2000
Location: Corsica
Posts: 552
|
Now, that's interesting:
Code:
$ perl $i = 1; $i += ++$i + $i++; print $i; 8 |
|
|
|
![]() |
| Bookmarks |
«
Previous Thread
|
Next Thread
»
| Thread Tools | |
| Display Modes | |
|
|
|
All times are GMT -7. The time now is 21:09.



















Linear Mode
