PHP Flow Control / Continue & Break
Subtitles of the Movie
In this lesson we're going to talk about how you can control the flow of PHP by use of the continue and break keywords. In the 0304.php file, we're going to start with a simple loop, which iterates 100 times and uses the variable I as our counter. Inside of our loop we're going to have a simple selection statement which checks to see if I is between the range of 50 and 75. If it is, we're going to output this long string. If not, we'll output a short string and in either case when we're finished with our selection statement, we're going to output the value of I, followed by an HTML break tag. Let's view this in the browser to see our results before we make changes. And here you can see all the outputs of I. And, in between the range of 50 and 75, you can see the long string output. And the loop quits iterating when I reaches 99. Now, let's see how we can control the flow of logic inside of this loop by using the continue keyword. Here I'll say if I is greater than 70, then continue. And what PHP is going to do is if both these conditions are met, it's going to continue on with the next iteration without executing any more PHP code. So the continue keyword allows you to skip any other execution of PHP code inside the current loop and continue on with the next loop. Let's refresh the browser and here you can see that when I was greater than 70, it stopped outputting until I was equal to 75 and then it continued on with the short output text. We can also change the flow of logic inside this loop by using the PHP break keyword. Here I'll say if I is greater than 90, break. The break statement is very simple and very much like the exit loop if script step inside of FileMaker Pro. Basically, when the condition is met, PHP is going to stop this current loop and continue on outside of the loop. So, in other words, we'll be breaking out of the loop. Here I'll put some more PHP code so that you can see that it continues on executing the rest of the script. Let's go ahead and refresh this in the browser to see our results and you can see that we broke out the loop when I was equal to 90 and continued on with more PHP code. In the event that you have a nested loop, which I'll create right here by saying X is equal to zero as long as X is less than four and we'll increment X by one. And encapsulate our current loop. You can break out of either one level of loops or out of more than one level by using the keyword break followed by the number of levels you want to break out of. For instance, let's go ahead and have the output of X displayed here inside of our echo statement so that we have context when we view this in the browser. And then let's add another condition and say if X is equal to two and I is equal to 24, then break space two. Here PHP will look for the condition to result to be true and if so, it's going to break out of not just the current loop but the second loop as well. When we refresh this in the browser, you can see that when X was equal to two and I was equal to 23 we exited out of both loops and outputted our remaining PHP code. So the break statement allows us to exit out of our loop but passing the number after the break statement allows us to exit out of more than one nesting of loops. If you remember, we used the break statement also inside of our switch selection statement to break out of not only this current switch statement, but any other encapsulating switch statements or looping statements. This concludes our lesson on talking about how you can control the flow of logic by using the continue and the break statements inside of PHP.
Tutorial Information
| Course: | FileMaker 9 & PHP Foundations |
| Author: | Lance Hallberg |
| SKU: | 33786 |
| ISBN: | 1-933736-99-2 |
| Release Date: | 2007-08-22 |
| Duration: | 9.5 hrs / 107 lessons |
| Work Files: |
Yes |
| Captions: | For Online University members only |
| Compatibility: |
Vista/XP/2000, OS X, Linux QuickTime 7, Flash 8 |
VTC Sign up & Benefits
- Unlimited Access
- 81,350 Video Tutorials (20,800 free)
- Video Available as Flash or QuickTime
- Over 782 Courses
- $30 for One Month Access
- Multi-User Discounts Available
United States 