Noob question about difference in loops

Hi All,
I am taking an online intro PHP class and am having a hard time understanding why the following two statements don’t output the same thing. The While loop outputs 0. 1. 2. 3. 4. FIVE 6. 7. 8. 9. 10. including the string “FIVE” as I expected, but the for loop does not. Instead it outputs 0 .1 .2 .3 .4 .5 .6 .7 .8 .9 .10 .11 Following are the two statements. Any help would be great because I am missing something super obvious.

While Loop:
<?php
$count = 0;
while ($count <= 10) {
if ($count == 5) {
echo “FIVE”;
echo “<br />”;
} else {
echo $count . ". ";
}
$count++;
}
?>

FOR Loop:
<?php
for ($count=0; $count <= 10; $count++) {
echo $count . " .";
} if ($count == 5) {
echo “FIVE”;
echo “<br />”;
} else {
echo $count . ". ";
}

?>

Jim

Hi jskintauy ,

The problem is quite simple actually, your for loop isn’t formatted the same as your while loop. You have your IF statement outside of the loop for example.

Below is the for loop that matches your while loop

<?php
for ($count=0; $count <= 10; $count++) {
    if ($count == 5) {
        echo "FIVE";
        echo "<br />";
    } else {
        echo $count . ". ";
    }
} 

?>

Wow, that was easy now that I see the changes. Thanks so much. i could not see that the IF statement was outside the loop to save me even though I knew it wasn’t being considered given that it provided ELSE even though the IF was true.
Doh,

Thanks so much

Jim