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. |
Darin McGrew |
Mar 25 2012, 11:29 PM
Post
#2
|
WDG Member Group: Root Admin Posts: 8,365 Joined: 4-August 06 From: Mountain View, CA Member No.: 3 |
Let's walk through what happens. We start with ($counter == 0):
$counter = $counter++; First, $counter is incremented by the ++ operator, thus ($counter == 1). Next, the original value of $counter (that is, 0) is returned by the ++ operator. Then that value is assigned to $counter by the = operator. Let's look at it again with the working statement: $counter++; First, $counter is incremented by the ++ operator, thus ($counter == 1), just as before. Next, the original value of $counter (that is, 0) is returned by the ++ operator, just as before. However, that original value is then ignored, instead of being assigned to $counter the way it was before. |
Lo-Fi Version | Time is now: 19th April 2024 - 02:50 PM |