Making Choices / Using 'Else' and 'Elseif'
Subtitles of the Movie
In this movie, we’re going to look at more complex conditionals, so far we’ve only seen what happens with a simple ‘if’ statement and if we put our condition in, we put a statement in to be executed if the condition is true and then any other commands we put in here are going to be carried out regardless of anything to do with our conditional. What if we wanted to allow php to go down two different paths, one if this condition is true and one if the condition is false, we use the else statement. This allows php to follow this path if this condition is true but we have an alternative here which allows us to have a statement that will only be carried out if this statement is not, so let’s try that, see it in practice and let’s set this number here to 2 and let’s refresh the screen, not more than 5. So in our example, this was the statement that was carried out and this statement was not. So we can see here the php has made a choice based on this condition and went down this path and not this one. Now we can make it more complicated still, for instance the 'else if' command. This can be written two ways, we can either write it as two separate words which is perfectly acceptable or we can write it together as in 'else if' command. Whichever way you choose is completely up to you. Now when we use our 'else if' command, the 'if' statement here works just like the first 'if' statement, we use the rounded bracket after it and then we put in another condition. So in this case, it’s going to be if num equals 5, then we'll use a statement that makes sense here, for instance equals 5 and then we close that, enclose that statement there with another curly bracket there. Once again we could put any number of statements here such as 'yes it does' for instance. Of course we could use different kinds of statements using the echo command because that's the only one we’ve learned so far. We’ll go into more complex commands later on. Now after that we could just use a plain ‘else’ and logically if num is not greater than 5 and is not equal to five, then it’s probably less than 5. So let’s try this one out and see what we get. Less than 5 and of course if we start with different values for num, we'll get different outcomes, equals 5. So we can see in this example how our ‘if’ statements can be quite clever and allow for quite a different number of outcomes based on in this case, the value of a variable. We can get more complicated than this and let’s go back to here, cut that out and what we can do to allow for more complex system of conditions is what's called nesting our ‘if’ statements. Now this is where the indents come in really handy so that we can see exactly what we’re doing. As we saw in the example, I’ve just wiped out, each of the statements here was indented. Now this is not strictly necessary for php to carry out the code properly but it helps us as human beings because we can then see a clear distinction between the conditions here and the statements which php will choose depending on whether they’re true or false. Now here’s where the indentation really comes into its own. We’re going to nest two 'if' statements and what nesting means is we put one 'if' statement inside the curly brackets of another. So in this case, if num is greater than 5, then we evaluate this 'if' statement which says if num is greater than 10, we’re going to open another set of curly brackets and then we’re going to indent twice and that's going to say more than 10, and then we can put an ‘else’ command in there and we can say more than 5 but less than 10. Now first of all let's save that, now let’s see how php works through the script and gets to where it should be. So if we took the number 7 and it gets to the first 'if' statement here, evaluates it to true because it’s greater than 5, it gets the second 'if' statement, looks at that, is 7 greater than 10, it says no, so it’s not going to carry out this statement, it goes down to the ‘else’ statement here and it’s going to carry out this one because it’s greater than 5 and it’s not greater than 10. So let’s test that out, make sure it works, there we go, that's our first nested conditional script. Once again I recommend you try these out your self and get to grips with exactly how they work and try making your scripts, try making your conditions as complicated as you like, if it stops working the way you expect it to, then strip it back and simplify it but just get a feel of the way these conditions work and see how to script them for yourself before we move on to the next point.
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
- 70,000 Video Tutorials (14,250 free)
- Video Available as Flash or QuickTime
- Over 650 Courses
- $30 for One Month Access
- Multi-User Discounts Available
United States 