Username: 
Password: 
PHP Tutorials

Making Choices / Introducing Conditionals: 'If' and Conditional Operators

Subtitles of the Movie

[00:00:00.0] In this chapter, we’re going to look at the ways in which php makes choices, we’re going to write some scripts in which php will decide whether [00:00:14.0] to go one way or another way within the script depending on whether certain conditions are met or whether they are not. [00:00:20.0] This will allow scripts to be more flexible and allow for multiple outcomes depending on the data, which is input. [00:00:29.0] So first of all we’re going to look at the basic if statements in this movie and we’re also going to look at the conditional operators, [00:00:39.0] which allow us to define the conditions whereby the if statements will choose one path or another within the script. [00:00:47.0] We’re going to look at logical operators, which will allow us to pair two or more conditions and allow the php script to only take one path [00:01:01.0] if both are true or if either are true and so on. We’re going to look at 'else' and 'else if' clauses, these allow for other clauses if [00:01:12.0] the principle 'if' statement is not true, we’re going to look at switch statements which is another way of making choices in php [00:01:19.0] and the other keywords go along with switch statements and finally we’re going to look at the ternary operator, [00:01:27.0] which is a very concise way of expressing a conditional and making a choice within a php script. [00:01:34.0] So let’s get right in and write our first conditional php script. As always we open our php tag and let’s define a variable called num with a value 7. [00:01:55.0] Now here’s how we use a conditional. We use the 'if' statement and then we use the regular brackets, the regular round brackets, [00:02:08.0] you’ll find them on the 9 and 0 keys and then we put our condition inside. So we’re going to say, if the variable num [00:02:17.0] then we’re going to use a little symbol you may or may not have seen before and this is the greater than symbol. [00:02:27.0] Put the number 5 in and close the bracket and this here is our condition. So if this condition being the variable num, [00:02:43.0] the value of the variable num being greater than 5, if that condition is true, then php will want to carry out another statement. [00:02:56.0] So in this case let’s say ‘echo num is more than 5’, let’s close our php script and we'll save this to examples as conditional. [00:03:19.0] Now let’s take a look at our browser, make sure the script works, as always we use local host to get to the right place and as we can see, [00:03:46.0] the answer has come out as we expected. Num is more than 5. Now if we wanted to enclose more than one statement within the conditional here, [00:03:56.0] we could use curly brackets like so, this means that anything that falls within these curly brackets is to be carried out if this statement is true. [00:04:19.0] It's usually a good idea to indent. Now whatever statement we put afterwards is going to be evaluated whether or not the condition is true [00:04:40.0] So in this example, we save that, in this example, let’s say the condition wasn't true. [00:04:53.0] Let’s say num was 2 and save that again and lets take a look at what happens here. [00:05:01.0] As we can see this gets printed out anyway as it’s the only text that’s echoed to the browser. [00:05:08.0] The reason for that is this condition is checked or evaluated and in this case it evaluates to false because 2 is not greater than 5 [00:05:19.0] and so this line, whatever comes in between these curly brackets is ignored and the php will proceed to the next line, which is this line and carry out [00:05:32.0] only this line. Now let’s take a look at the operator that we used here, so far we’ve only used the greater than operator, there are quite a number of [00:05:44.0] different operators, however there is also the equals operator. Now this may look a little strange, it may look like I’ve made a mistake here. [00:05:52.0] Why have I typed the equals sign twice? Well as we learned before, this sign in php and a lot of other programming languages is not considered an [00:06:03.0] equal sign on its own. This is the 'is set to' sign that's because what it does is it sets the value of the variable to the left to the value on the right. [00:06:16.0] if we want to compare two values and see if they are identical, then we have to use a double sign here, which is the equal sign. [00:06:28.0] Let’s see what would happen if we simply used the ‘set to’ sign. I’m going to add another line here to make sure it reads out the value of num. [00:06:50.0] Now to look at this, if we were to just look at it and use our common sense, it would look like this would never get evaluated because [00:06:58.0] this statement is not going to be true, however we have to think a little differently in this case. [00:07:07.0] Num is more than 5 and it’s printed out the number 5, now if we looked back at our php script, we can see that apparently it evaluated this expression as true. [00:07:23.0] Now how can this be, well the answer is that if we use the 'is set to' operator then php simply sets num to 5 and then evaluates the whole statement as true [00:07:39.0] even though it’s within a conditional. So we must always remember, if we’re going to use this equals sign as a comparison operator, [00:07:50.0] then we must make sure to use the right format, which is the double sign otherwise php is going to interpret it as the 'is set to' sign and [00:07:59.0] we’re going to end up with no end of trouble. This is a mistake that I’ve made so many times in the past, [00:08:04.0] so if you find yourself making it a few times, then you’re in company. Let’s have a look at some more comparison operators. [00:08:12.0] We’ve seen the ‘is greater than’, we have seen the equals and we have seen the ‘is less than’, which is similar to is greater than but the other way around [00:08:24.0] there’s also ‘is less than or equal’ to. So if num was 5, then in this case this condition will be true. [00:08:35.0] However if we simply use the ‘is less than’, then it will be looking for a number less than 5 and so the condition would evaluate to false. [00:08:46.0] We also have of course the greater than or equal sign and finally we have the un equal sign. [00:08:56.0] This will only evaluate true when num is not equal to 5. [00:09:06.0]

Tutorial Information

Title: 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