Now that we've talked about looping in PHP, the For Loop, the While Loop and the Foreach Loop in use with arrays, let's talk about how we can control the flow of code inside those loops. And specific we'll be talking about the Continue Statement and the Break Statement. Both of these statements allow us to be able to skip code if certain conditions are met or to exit the loop early if certain conditions are met. I'm going to use this example in the project file 0306.php. Open in my code editor and I have a simple For Loop which is basically iterating through the variable I a 100 times and outputting that to the browser. So let's go ahead and refresh our browser and we can see that I is being outputted here followed by a break tag. And now let's experiment with the Continue Statement. Let's say that I want to skip outputting I if it's value is between 25 and 50. So I'm going to go ahead and say if I is greater than 24 and I is less than 51. So the condition would be true if I is 25 through 50. Then what do I want to do? Well I'd like to continue. Now what that does is that tells PHP to no longer execute any code within this loop after the continue statement. So what it'll do is skip this echo and go back up and increment I and continue on with the For Loop. So in other words we should not see any of the values 25 through 50 outputted to the browser. I'll save this and refresh it and now if we get down to the value 24 you can see that there is nothing past 24 until we get to 51. So that's how you use the Continue Statement. Now let's talk about the Break Statement. What the Break Statement does is allow you to jettison a, a loop early if a certain condition is met. And how that works is you say if the value gets to a certain point or you put some kind of test or condition in your IF Statement, then go ahead and break. So if I becomes greater than 24 it's going to break and really this will never get evaluated. So we'll just leave it as I is greater than 24. So we should stop at 25 and you will not see 25 outputted. We refresh and we stopped at 24. So that's the Continue Statement and the Break Statement. And one more thing I'll mention before we stop this is that you can use the Break and Continue Statement within nested loops. For example I'm going to go ahead and nest this loop inside of another For Loop. We'll say that X is equal to one and as long as X is less than four then increment X and we'll go ahead and close that nested loop. So now we're going to be looping within inside of another loop. So when you say break, are you going to break this loop or are you going to break the one that iterates through X as well? If you leave break by itself it's just going to break the I loop but it's still going to process the code inside the X loop. And we'll show that example here. So you can see that X is continuing on until it's gone through it's loop four times. But if I say break two then that's going to break up into the second loop on the outside. So if I hit Refresh you can see that we've stopped. So you can specify which loop if you're nested and you can do that as well with the Continue Statement. So this concludes talking about how you can control the flow in your loops using the Break Statement and the Continue Statement.
| Course: | FileMaker and PHP Foundations |
| Author: | Lance Hallberg |
| SKU: | 34393 |
| ISBN: | 978-1-61866-077-0 |
| Release Date: | 2012-12-07 |
| Duration: | 7.5 hrs / 97 lessons |
| Work Files: |
Yes |
| Captions: | No |
| Compatibility: |
Vista/XP/2000, OS X, Linux QuickTime 7, Flash 8 |