Please explain

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
cybershot
Forum Commoner
Posts: 29
Joined: Thu Jul 24, 2008 12:06 pm

Please explain

Post by cybershot »

I am learning php. I am working with a loop

Code: Select all

for($count = 0; $count <= 10; $count++)
	{
		if($count == 5)
		continue;
	}
	echo $count . ", ";
this is suppose to print 0 - 4 and 6 - 10 skipping five. but what it does is echo 11. I don't understand why
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: Please explain

Post by Celauran »

Are you sure you didn't type $count < 10 by mistake? The code as it is written in your post does precisely what you describe.
cybershot
Forum Commoner
Posts: 29
Joined: Thu Jul 24, 2008 12:06 pm

Re: Please explain

Post by cybershot »

you lost me
User avatar
McInfo
DevNet Resident
Posts: 1532
Joined: Wed Apr 01, 2009 1:31 pm

Re: Please explain

Post by McInfo »

Here is your code with slightly different formatting.

Code: Select all

for($count = 0; $count <= 10; $count++) {
    if ($count == 5) {
        continue;
    }
}
echo $count . ", ";
The loop essentially accomplishes nothing except to increment $count to 11. After the loop is done, the echo statement executes one time.
Last edited by McInfo on Sun Nov 21, 2010 10:20 pm, edited 2 times in total.
cybershot
Forum Commoner
Posts: 29
Joined: Thu Jul 24, 2008 12:06 pm

Re: Please explain

Post by cybershot »

right, but I copied it from a video tutorial and the tutorial clearly shows that code echoing 0 - 4 6 - 10. so i do not understand why it doesn't seem to execute the if statement
User avatar
McInfo
DevNet Resident
Posts: 1532
Joined: Wed Apr 01, 2009 1:31 pm

Re: Please explain

Post by McInfo »

The explicit continue statement has no noticeable effect because it appears at the bottom of the loop and continue is already always implied by the bottom of the loop. In other words, there is nothing to skip by explicitly jumping to the next iteration of the loop. Continuing in a for loop does not skip the iteration expression ($count++). The echo statement is not inside the loop.
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: Please explain

Post by Celauran »

I can't believe I missed it earlier. It's the positioning of the braces which is what's causing yours to print only 11.

Your code:

Code: Select all

for($count = 0; $count <= 10; $count++)
       {
          if($count == 5)
          continue;
       }
       echo $count . ", ";
The code that behaves as you described:

Code: Select all

for($count = 0; $count <= 10; $count++)
{
    if($count == 5)
        continue;
    echo $count . ", ";
}
For clarity, use braces around the conditional as well:

Code: Select all

for($count = 0; $count <= 10; $count++)
{
    if($count == 5)
    {
        continue;
    }
    echo $count . ", ";
}
cybershot
Forum Commoner
Posts: 29
Joined: Thu Jul 24, 2008 12:06 pm

Re: Please explain

Post by cybershot »

man. I don't know how I missed it either Celauran. That is what I was looking for. Thanks everyone.
Post Reply