Printable Version of Topic

Click here to view this topic in its original format

HTMLHelp Forums _ Server-side Scripting _ PHP Question - Small Script

Posted by: Aush Mar 24 2012, 07:37 PM

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.

Posted by: Darin McGrew Mar 24 2012, 08:08 PM

Do you understand what $counter++ does?

Posted by: Aush Mar 24 2012, 08:48 PM

I thought it was supposed to add 1 everytime it looped.

Posted by: Ephraim F. Moya Mar 24 2012, 09:31 PM

QUOTE(Aush @ Mar 24 2012, 05:37 PM) *

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.

Posted by: Aush Mar 25 2012, 01:13 PM

Thank you for the response. smile.gif

I still have a question, though. Even if $counter is incremented after using it, shouldn't it still add 1 each time, since it's looping?

I don't see why it makes a difference whether it's used before or after.

Posted by: Ephraim F. Moya Mar 25 2012, 01:55 PM

QUOTE(Aush @ Mar 25 2012, 11:13 AM) *

Thank you for the response. smile.gif

I still have a question, though. Even if $counter is incremented after using it, shouldn't it still add 1 each time, since it's looping?

I don't see why it makes a difference whether it's used before or after.


Because $counter = $counter++ IS ALWAYS ZERO!

You might be interested in this more efficient way of writing your loop:

<?php

for( $start = 1, $counter = 0; $start < 11; $start++ )
{
echo "{$counter++}. <br>";
}

or:

for( $start = 0, $counter = 0; $start < 10; echo "{$counter++} <br>", $start++);

Posted by: Ephraim F. Moya Mar 25 2012, 10:42 PM

QUOTE(Ephraim F. Moya @ Mar 25 2012, 11:55 AM) *

QUOTE(Aush @ Mar 25 2012, 11:13 AM) *

Thank you for the response. smile.gif

I still have a question, though. Even if $counter is incremented after using it, shouldn't it still add 1 each time, since it's looping?

I don't see why it makes a difference whether it's used before or after.


Because $counter = $counter++ IS ALWAYS ZERO!

You might be interested in this more efficient way of writing your loop:

<?php

for( $start = 1, $counter = 0; $start < 11; $start++ )
{
echo "{$counter++}. <br>";
}

or:

for( $start = 0, $counter = 0; $start < 10; echo "{$counter++} <br>", $start++);


I found an error in my second example above. It should be:

<?php
for( $start = 0, $counter = 0; $start < 10; print "{$counter}<br>", $counter++, $start++ );
?>

The double brace {} can't have any computation.
Darn!

Guys - Can an entry be edited after the [Edit] button disappears? If so, how?

Posted by: Darin McGrew Mar 25 2012, 11:29 PM

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.

Powered by Invision Power Board (http://www.invisionboard.com)
© Invision Power Services (http://www.invisionpower.com)