PHP Question - Small Script |
PHP Question - Small Script |
Aush |
Mar 24 2012, 07:37 PM
Post
#1
|
Novice Group: Members Posts: 25 Joined: 8-February 10 Member No.: 11,027 |
I have a very small script that I need help with. I'm learning PHP, and I can't figure out why this loop won't work. Please help! =]
Here it is: <?PHP $counter = 0; $start = 1; for($start; $start < 11; $start++) { $counter = $counter++; print $counter . "<BR>"; } ?> It DOES work when I do this, however: <?PHP $counter = 0; $start = 1; for($start; $start < 11; $start++) { $counter = $counter + 1; print $counter . "<BR>"; } ?> I bolded the difference between the two. Thank you very much. |
Ephraim F. Moya |
Mar 24 2012, 09:31 PM
Post
#2
|
Advanced Member Group: Members Posts: 167 Joined: 2-September 07 From: New Mexico Member No.: 3,702 |
I have a very small script that I need help with. I'm learning PHP, and I can't figure out why this loop won't work. Please help! =] Here it is: <?PHP $counter = 0; $start = 1; for($start; $start < 11; $start++) { $counter = $counter++; print $counter . "<BR>"; } ?> It DOES work when I do this, however: <?PHP $counter = 0; $start = 1; for($start; $start < 11; $start++) { $counter = $counter + 1; print $counter . "<BR>"; } ?> I bolded the difference between the two. Thank you very much. With the ++ after the variable ($v++) the variable is used first then it's incremented. With the ++ before the variable (++$v) the variable is incremented before use. So What you want is $counter = ++$counter; Or maybe just $counter++; by itself. Or ++$counter; by itself. |
Lo-Fi Version | Time is now: 24th April 2024 - 03:51 AM |