Loops and Control Structures / Get Me Out Of Here! 'Break' and 'Exit'
Visitors to VTC.com will be able to view all introductory videos for each training course.
Free Trial Members will gain access to first three chapters for each training course.
Full Access Members have full access to VTC.com’s entire library of video tutorials.
Learn More
Subtitles of the Movie
Now we’re going to look at 2 commands, which allow us not to make loops but to get out of loops or to get out of scripts. First of all let’s look at the 'break' command. Now we touched on this very briefly in the context of the switch condition and let’s see how this works with loops. Let’s set up a loop similar to the way we did in the first movie in this chapter, and this time we’re just going to echo, echo the number of 'i', value of 'i' rather, and then we’re going to put in a condition here which is an 'if' statement saying 'if' variable 'i' equals remember to put that double sign in for equals 14, then we want to break. We close our loop and once the loop’s finished, we’re going to put in another little echo statement reminding us at what point the loop ended. O.K let’s save that and see what this does. Refresh our browser, goes from zero, adding 1 to 'i' each time and echoing 'i' to the browser and finally gets to 14, and we see this line, loop ended at 14. So what happened there? This line allows php to escape from this loop because if we see this condition up here, the loop is supposed to go on until 20. We’ve put in an extra condition here that says when the loop is equal to 14, then we’re going to ignore this 'for' loop business we’re going to jump straight out to the end, to the outside of this curly bracket here, then we’re going to execute the next statement, which in this case is echo, another echo command and its going to tell us at what point the loop ended. So we can see that break is a way of adding conditions within a loop that allow us to get out. The other command I want to show you in this movie is the exit command, so let’s take off those commands there and I think the easiest way to show this simply by using two echo commands, that will simply say this text will be shown and I’m going to use the exit command remember to put semi colon at the end of the lines and then we’re going to say this text wont and save that, and check it out in our browser. So the only text that is shown is the text from the first echo statement. So as we can see what exit does as a command is it effectively tells php to ignore every thing below it, tells php to stop processing this entire script and jump right down to this close php tag and stop. So there are two other control statements that we can use, exit is slightly more drastic than break. Both are very useful and we’ll find uses for them as we start writing more complex scripts. That's the end of this chapter and in our next chapter we’re going to look at ways of manipulating data, which is what php and others other middle ware is all about.
Tutorial Information
| Course: | PHP |
| Author: | Joshua Mostafa |
| SKU: | 33332 |
| ISBN: | 1889347787 |
| Release Date: | 2002-03-26 |
| Duration: | 9.5 hrs / 92 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
- 98,729 Video Tutorials (23,265 free)
- Video Available as Flash or QuickTime
- Over 1026 Courses
- $30 for One Month Access
- Multi-User Discounts Available
United States 