Making Choices / Multiple Choices: 'Switchcasebreak'
Subtitles of the Movie
There is another command that we use in php as an alternative to the 'if' and 'else' statements to carry out complex conditionals. This is called a switch statement. Let’s take a look in our script, we’ll start with exactly the same variable declaration and then we’re going to use the switch statement to do something similar to what we’ve been doing before in a slightly different way. We use the switch statement and then inside the round brackets, we put num, we don't put any other conditions, we just put it on its own and then we open up our curly brackets, then we use a command called case and then straight after that no brackets, we put a value that the variable might be, so for instance case 5. So what this is saying is if num is 5 then carry out this. So in this case, we say num is 5. Let me use a statement break with a semi colon, you’ll notice that after case 5, we put a regular colon, not a semi colon This is a way of saying that these statements that follow it, are dependent on this condition being true. We can put another condition straight after the break, what the break does, the break is a way of saying stop evaluating these conditions such as case 5 and case 10, stop evaluating them and skip down to the curly brackets that are going to come at the end. So if case is true, then it carries out this statement, echoes num is 5 to the browser, goes to break and falls of the end. Case 10, we’re going to say echo num is 10, it's a good idea as well something I forgot to do to indent these. Then we can see what's happening and lastly we’re going to put case 7, so if num is 7, which is what it is, then we’re going to echo num is 7 to the browser and that's our lot. So we save that and then we’re going to take a look and make sure that works num is 7, so as we saw before, it evaluated num, checked what value num had and if it was 5 then it would do this, but it wasn't so it went down to the next one, if it was 10 that it would do this but it wasn't so it went down to the next one, if it was 7 then it carried out these statements. Forgot the closing php tag there. O.k. there is another statement that is involved in the switch, in the switch conditional and that's called the default statement. Say if we choose a number that was neither 5,10 or 7 say it was 36, then we could simply keep adding case statements here in order to account for all possibilities, but that could get rather tedious. So instead we can simply put in a default case, which is what happens, we put that at the end of all the other cases and this is what happens if all the others are untrue and it really doesn't matter what num is, this following statement will be carried out, so we’re going to say m is not a num, rather is not 5, 10 or 7. Let’s save that and we’ll check in the browser, see if it works, so we refresh and as we can see, it’s chosen this statement. So it's checked this statement, this statement, this statement, they have all been false, so it's gone down to the default and its carried out this one. Now you may notice certain similarity between the 'if' statements, ‘if’ and ‘else’ statements and the switch statement and you may be wondering why there are so many different ways of doing similar things. The answer is that as you develop more different kinds of scripts and you have more different kinds of choices that have to be made within your php scripts, you will find that switch statement and the 'if else' and 'else if' statements all have their own uses in which for the reasons of making it clear to the reader, it’s easier to use 'switch' or 'if.' To the computer, it makes absolutely no difference and you’re completely at liberty to use which ever you like. Just showing them all here so that you get a good idea of the comprehensive range of ways that php makes decisions. There's just one more left, that's a very concise little one and very useful and we’re going to find out about that in our next movie.
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
- 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 