Making Choices / The Concise Conditional: '?' :
Subtitles of the Movie
And now we’re going to look at a different way of making decisions, which is not even a statement but an operator. Operators as we saw before were the little symbols that php uses to express different kinds of conditions or rather that we use to express to php what we want it to do. So let’s have a look at, first of all we’re going to do this, we’re going to do this condition the way we learnt before, using 'if' and an 'if' and an 'else' clause. Now that seems fairly concise way of putting it and we’re going to set our num up here. o.k. so you should be able to work out what this one does, you’ve seen quite a similar things in the previous movies. If the variable nums value evaluates to 7, then we echo exactly 7 to the browser, in all other cases we echo, I’ve put a little typo in there not 7, o.k. Now there is a conditional called the ternary conditional, which is a very concise way of expressing the same thing all in one line. This may look a little intimidating because it's a lot more concise but don't be scared, you don't need to use it right off, just use the 'if' statements until you get completely happy with it and then later on you’ll see why its such a useful way of writing things. So we start of with our echo statement, this a completely different way around of working because in this way around, we write the condition first and then we have to write two echo statements here, this way if we write the echo statement and then inside the brackets here, we put the conditions so if num equals 7, then we use a question mark and this is a first half of the ternary operator, and what that means is it's exactly the same as an 'if' statement in the way it works, and we type in what we want to echo to the browser if this condition is true we type it here, then we put a colon, not a semi colon, a colon and then we put in the else condition afterwards. So it works very much like an 'if else' condition and let’s take this out because we don't need this. Now this one line of code does the job of those five lines of code we had before. Let’s save it, make sure it works, not 7 and the value that we choose in this case was 8. So our little ternary condition, very concise but it works just fine. Let’s have a little look in more detail at the way it works. First of all we have the echo statement or what ever command we want to use and then instead of simply writing the text to be echoed straight after it, we put in this condition and that's a way of telling php that hold on a minute, don't echo right now. First of all evaluate this and then based on that, choose which one of these you are going to put in there, it’s as if this text is swapped in in place of this condition, either this text or this text. So this question mark is like the true side of the operator and this colon is like the false side. So if it's false or the else, a ternary operator is expressed like this. But we never see it just like that, we always see it within the context of a line Now this can get really useful when for instance we have several 'if else' statements and say if we wanted to put in our ‘else’ clause here. If we wanted to put another condition, this is a way of simulating an 'else if' but again all just in one line. We put another condition here and if that’s true, then we move on to this one and if not we’ll do that one, again we use the colon there for ‘else’ and then a little semi colon at the end. Let’s save that and make sure that works. More than 7, and 8 is more than 7, so our little script must be working. I’ll just go briefly through the way this works, this beginning bit works in a similar way, we get to the echo statement and this conditional says wait up, evaluate me first and then see what text you want to swap in. So first of all it says, is this true, if it's true then we want to swap in exactly 7 the value is 8, so its not true. So we move on to the second part of the ternary, first ternary condition and it wants to swap something back into here to be echoed but only finds another condition. So you’re going to have to wait a little bit longer. The echo condition is going to have to wait a little bit longer to get it’s result and the condition this time is if num is greater than 7 and if that's true as in this case, then it prints out this and if not then it prints out that. Now needless to say, this takes up a lot less space than writing, let's see how many would we need, we need an 'if' statement here and then an 'else if' and then another 'else', so that would work out to about 7 or 8 lines of code. So we can see the ternary operator is a very concise way of expressing conditions in php script. As I said before, if you don't understand how the ternary operator works or if you find it too intimidating this concise way of coding, don't worry, you don't need to use it right now. Just play with the 'if' statement and if you find this useful, by all means go ahead and use it. But you can write script as complex and you like and never have to use a ternary operator or never have to use a switch operator either. Once again it's a matter of personal preference and go with what you like. So that wraps it up for this chapter on conditionals and in our next chapter we’re going to look at more complex control structures and ways of repeating and iterating elements within our PHP scripts.
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 