Sign In | Subscribe
Start learning today, and be successful in your academic & professional career. Start Today!
Loading video...
This is a quick preview of the lesson. For full access, please Log In or Sign up.
For more information, please see full course syllabus of Introduction to PHP
  • Discussion

  • Study Guides

  • Download Lecture Slides

  • Table of Contents

  • Transcription

  • Related Services

Bookmark and Share

Start Learning Now

Our free lessons will get you started (Adobe Flash® required).
Get immediate access to our entire library.

Sign up for Educator.com

Membership Overview

  • Unlimited access to our entire library of courses.
  • Search and jump to exactly what you want to learn.
  • *Ask questions and get answers from the community and our teachers!
  • Practice questions with step-by-step solutions.
  • Download lesson files for programming and software training practice.
  • Track your course viewing progress.
  • Download lecture slides for taking notes.
  • Learn at your own pace... anytime, anywhere!

More Conditional Control Structure

  • The elseif statement is a conditional control structure that can be associated with an if statement. If an if statement’s test condition is FALSE and an associated elseif statement’s test condition is TRUE , the elseif’s statement group gets executed.
  • Multiple elseif statements can be associated with a single if statement.
  • An else statement can be appended to the end of a series of if/elseif statements so if none of the if/elseif test conditions evaluate to TRUE , then the else statement group will be executed.
  • A switch() statement is used to compare a single test expression against different values, known as cases , and then evaluate any code associated with the case it matches.
  • Case values can only be expressions that evaluate to one of the scalar data types (int, float, string, bool) or NULL .
  • A switch() statement executes all statements it encounters after a matching case is found until either a break statement or the end of the switch() statement is reached, whichever occurs first.
  • A default case can be assigned to a switch() whose associated statement(s) will be executed if the test expression did not match any case in the switch().
  • Additional Resources:

More Conditional Control Structure

Lecture Slides are screen-captured images of important points in the lecture. Students can download and print out these lecture slide images to do practice problems as well as take notes while watching the lecture.

  • Intro 0:00
  • Lesson Overview 0:13
    • Lesson Overview
  • elseif Statement 0:45
    • elseif Statement
    • elseif Statement Coding Example
  • Multiple elseif Statements 2:16
    • Multiple elseif Statements
    • Multiple elseif Statements Coding Example
  • Adding an else Statement 5:44
    • Adding an else Statement Overview
    • Adding an else Statement Coding Example
  • switch() Statement 8:07
    • switch() Statement
  • switch() Statement (Cont.) 9:14
    • switch() Statement (Cont.)
    • switch() Statement Coding Example
  • default Case 14:20
    • default Case
    • default Case Coding Example
  • Coding Conventions 15:57
    • Coding Conventions
  • Coding Conventions (cont.) 17:10
    • Coding Conventions (cont.)
  • Homework Challenge 18:06
    • Homework Challenge
  • Homework Challenge (cont.) 19:18
    • Homework Challenge (cont.)

Transcription: More Conditional Control Structure

Hello again, and welcome back to Educator.com's Introduction to PHP course.0000

In today's lesson, we are going to be introducing some more conditional control structures.0005

In particular, we are going to be introducing the elseif and the switch statement.0008

As mentioned, we are going to introduce the control structure that is called an elseif statement,0015

which is used in combination with an if statement, which you have already learned about.0020

We are going to learn about a switch statement, which allows you to evaluate different statements, based on the value of a certain variable.0026

And we are going to talk about something known as the default case, which applies to switch statements.0034

And then, we are just going to quickly go over coding conventions, as it relates to these two types of control structures we are introducing.0039

An elseif statement is a conditional control structure that you can associate to an if statement, like an else statement that we had learned about.0048

The difference between an else and an elseif statement--one thing I should mention, again, 0056

is that an elseif statement is technically not a statement, as far as PHP goes, but that is commonly what it is referred to as.0061

The difference between an else and an elseif statement is that the elseif, if the test condition for the if statement is false,0067

has another test condition it evaluates, and it only evaluates its code segment if that is true.0078

For example, this is an example of an if statement combined with an elseif statement.0083

Here, we have if, and then test condition 1--if test1 is true, the if statement group gets executed, and execution continues down to the bottom.0089

Now, if this test condition equals false, instead of going down to an else statement, we go down to an elseif statement.0102

And this provides another test that we evaluate, 0109

and then this section associated with the elseif statement only gets executed if test2 is equal to true.0112

So, it allows us a way to allow another test condition to decide what type of code or which section of code to evaluate on.0119

So again, this section is only going to get evaluated if test1 from the if statement was false, and then test2 was equal to true.0129

One of the nice things you can do with elseif statements is: you can chain multiple elseif statements together.0137

And you can do that as often as you need it.0144

In this case, we have an if statement with two elseif statements appended to it.0147

And the same rules apply as we just talked about: for example, this statement group only gets executed if test1 is false and test2 is true.0153

However, if it comes down here, test1 is false, and test2 ends up being false, it goes on to the next elseif block and evaluates test3.0162

If test3 is equal to true, then it evaluates test3's statement.0170

However, if none of the test conditions apply, then you just exit the entire control structure set, 0175

and you continue with whatever statement is next in line.0181

Let's take a look at an example of the elseif statement in action.0188

For example, this is a script I have written called elseif.php, and it accepts one GET variable called num, and you set that equal to a numerical value.0195

And what we have is a couple of elseif statements that will conditionally execute or echo a statement,0207

depending on the value of the number that is passed in.0215

For example, the first test is if num (and in our code, we have num as a short variable created for the GET variable num)0217

is equal to null (which means it wasn't passed in); then, we echo "no number was provided."0226

If num is less than 0, we write, "the number provided is negative"; if it's greater than 0, we write "the number provided is positive."0230

If none of these conditions are met (which is only the case if num equals 0), then we just move on to the next statement, which would be down here.0238

So, let's go ahead and show you how this works.0246

If we go ahead and put num=...well, first of all, actually, let's just look at the page where we don't put any input.0248

We haven't included any num statement: down at the bottom here, we can see the results.0260

This first result here applies to this first elseif statement: we can see the message "no number was provided,"0264

which means that num equals null, and this section was executed.0270

Let's go ahead and enter num=100, which is a positive number.0273

When we do that, what is going to happen is: this code that is going to be entered is going to say, "Is num equal to null?"0282

It's going to say false; num is equal to 100; "Is num less than 0?"--that test is going to return false.0289

And it's going to go down here and say, "Is num greater than 0?"0297

100 is greater than 0; this is going to evaluate to true, so this echo statement gets executed.0299

If you look down at results, it outputs that statement, "The number provided is positive."0304

Now, if we go up here, and we change this num to equal -100, and we hit Enter, the same process is going to happen.0309

num is not going to be equal to null, but what it is going to do is: when it reaches the first elseif statement,0317

it is going to say num is less than 0--that is going to evaluate to true, so this echo statement is going to be output.0322

And then, because this condition was met, this following elseif statement doesn't even get evaluated.0327

This test condition never gets evaluated, and execution would jump immediately to the following line of code.0332

If we look at the result, we can see "the number provided is negative."0338

Now, one of the things that we can do, as we can do with an if statement, is: we can add an else statement to a group of if and elseif statements.0345

And what happens is: the else statement group gets executed if none of the conditions in the original if,0354

or any of its associated elseif, statements evaluate to true.0360

And this right here is an example of this sort of structure.0365

It has an if statement, followed by an elseif statement, followed by an else.0370

What happens is: if test1=true, it goes ahead and evaluates this group and jumps to the bottom down here.0375

and evaluates the next line of code after all of these structures.0382

If test1 is false, it goes down here and evaluates condition test2.0385

As we know from what we learned about elseif, if this is true, then this statement group gets executed, and again, we would jump out to here.0390

Now, if test1 is false, and we go down here and test2 is false, then else gets executed, no matter what.0399

And in this case, it is going to execute whatever the else statement group is.0407

So, if we go back and look at our elseif.php file, we have another section of elseif statements here,0411

except--it is exactly the same as before, except we have added an else statement here.0417

And what we had talked about before was that this first group of if, elseif statements--0421

they will always enter at least one of these statements, unless the number is 0.0427

So, in this case, if it is equal to null, the first one is executed; if it's negative, the second one is executed; if it's positive, the other one is executed.0431

But if it is 0, it is going to be none of these; so in this case, we can add an else statement.0439

It says, "If you get to this section, the number you provided is 0," and it's going to say "the number provided was 0."0442

So, if we go ahead and input the number 0 and refresh the page, we can see two things.0449

We can see 1) the output for this bottom section with the else statement says "the number provided was 0," and that is because of our else statement.0459

We also can see that nothing was output for the first section of ifelse statements.0466

That is because 0 met none of these conditions: it wasn't equal to null, it wasn't less than 0, and it wasn't greater than 0.0470

So again, these elseif blocks only get evaluated if one of their conditions is true.0478

If not, execution just jumps to immediately afterwards.0482

The next control structure we are going to introduce is known as the switch statement.0489

What that does is: that compares a single test expression against a number of different values, which are called cases,0493

and then evaluates any statements associated with those cases.0502

For example, this is the syntax of what it looks like.0506

It's a switch keyword, and then it is followed by an expression in parentheses.0509

In this case, it's a variable called num that is going to hold a number value.0515

And then, what you can see here is: within the switch statement, which is denoted, 0521

as with the other control structures, by curly braces, it has a number of different cases.0526

It has case here, which is 1, that says, "If the value of num is equal to 1, then you process these statements here."0531

"If num is equal to 2, then you process these statements here."0542

"If num isn't equal to any of these, none of them get processed, and execution jumps out to the end of the script."0546

Now, one thing to note is that the case values (here is another example) can only be scalar values.0556

So, they can be strings, integers, bools, and floats.0564

Or, they can be the value null; they can't be things such as arrays and so forth, or objects.0570

Also, what happens is: on the last slide, you will notice that, 0576

after each statement in our case statement, we had a second statement that was break.0580

This break statement is a special statement, and what it does is: the way switch operates is:0586

you input a value or expression, and then it goes down linearly from top to bottom, evaluating each case, and saying,0592

"Is the value passed in equal to this case?"0599

And then, what it does is: once it finds a case that matches, it goes ahead and runs every statement, 0603

until it either reaches a break statement or the end of the switch statement.0608

So, in this case, because there is no break here after the first case statement, if the name passed in is the string joe,0614

what is going to happen is: it's going to say, "OK, I match here, so I'm going to evaluate this case."0621

"I'm going to evaluate this statement here," so it's going to echo "name is joe."0625

But then, there is no break statement; and as mentioned, the switch statement continues to execute statements0630

until it either reaches a break statement or the end of the switch statement.0634

So, there is no break statement here, so what it does is: it goes down here; it is going to output this line, as well.0638

It is going to echo "name is john"; then it is going to reach the end of the switch statement, and it's going to stop.0643

So, this is probably not the behavior that would be desired, and so that is where break statements come into use.0647

Typically, to make this function as we would want it to be, we would add a break statement below here, and then a break statement below here, as well.0655

Let's take a look at some code examples using the switch statement.0671

Let's go to switch.php, which is an example file.0679

And here, we have a switch statement that uses, as its value to compare against, the value of the variable called phone,0683

which, for this script, is a short variable for a GET variable that is passed in called phone.0695

We are going to have, for this script, that phone can take on the different string values home, work, or cell.0703

And so, what happens is: if we were to pass in a GET parameter, in this case, in this original page, we just have switch.php0711

with no query string, so phone is going to get set equal to null, because we have used a ternary operator to process the GET variable.0717

And the way we do that is: if the GET variable phone is set, we set it to the value; if not, we set it equal to null.0726

So, if nothing is passed in, we go down here; we reach the case null; it is going to echo "no phone number was provided."0733

And then, you notice the break statement here, which means none of the rest of these statements are going to get evaluated.0739

And so, it is going to jump to the end of this switch statement and break out of it, which is where the name "break" comes from.0748

If we look down at our results for the first output, it will say "no phone number was provided."0752

Now, for these other ones, it has a case for if you provide home; it's going to say "the phone number provided was home,"0759

"phone number provided was work" if you provide the name work, and so on for cell.0763

So, if we just go through and try out some of these...we'll say phone=home; you can see, down here at the bottom,0768

it says "phone number provided was home," and that echoes this statement right here,0775

because when phone variable was passed into this switch statement, it didn't match null, but it did match home.0780

And then, just to do one more as an example, we'll put in cell.0787

What is going to happen is: this switch statement is going to get evaluated; phone is going to be compared to null;0791

in this case, it's the string cell, so this is not going to be evaluated; it is not going to be equal to home,0798

so this case and its associated statements are not going to be evaluated.0802

It doesn't equal work, so this is not going to be evaluated; and then, at the same time, it does equal cell, so this one is going to get evaluated.0807

So, when we refresh the page, if we go look at the bottom, we can see "the phone number provided was cell."0814

Now, one other scenario: let's say we provided something that said cell2.0822

Maybe this is a script that processes different phone numbers, and it said, "I want to update my phone, cell2."0828

If we go ahead and enter that, you will notice that there is no output here.0834

That is because, when phone equals cell2, and it enters the switch statement, the switch statement is going to compare0839

the string cell2 to each of these different cases.0848

Because it doesn't equal any of these cases, none of these statements gets executed.0850

That is going to bring us to another interesting point we are going to talk about, which is called a default case.0855

This is similar to an else statement that gets appended to a series of if, elseif statements.0864

What that does is: it uses the keyword default, and what it states is that if you enter the switch statement, 0869

and you compare the expression passed in (in this case, the variable num), and it doesn't match any of the cases,0879

then by default, it is going to always run this statement group here.0886

So, in this example, if num equaled, for example, the integer 2, we would come down here, and we would say,0891

"case equals 0; 2 doesn't equal 0"; it wouldn't evaluate this.0897

It would compare 0 to 1; that would evaluate to false, so it wouldn't echo here.0901

Then, when it reaches the default section, and it says, "Well, it hasn't reached any of the others of these,"0905

we are always going to go ahead and output this here.0908

And we can make use of that in our script to go back here and...here we have a switch statement.0912

For this part right here, it is the same as we had before in this top example, except we have added a default case.0920

And so, what that is going to do is: as you notice, when we entered cell2 as the value of phone, 0926

you can see that nothing was output for this top if statement, because it fell through all of these cases and had no default output.0933

But down here, we have a default case that says, if you compare phone to each of these cases, 0941

and it doesn't match any of them, then you go ahead and output this default statement.0947

In this case, we are just echoing the statement that says "the phone number will be set to home."0950

And as you can see, that is what happened.0954

Just quickly, we are going to talk about coding conventions.0959

For elseif statements, the same coding conventions are going to apply as we use for if statements,0963

as far as parentheses and spaces and curly braces go.0968

The switch statement, also, is going to have similar code formatting.0971

If we go and look at the code, the one thing you will notice is: like with our if statement, 0976

there will always be a space between the switch keyword and the opening parentheses of the test condition.0980

The opening curly brace will be on the same line as the switch statement, and there will be a space between that and the closing parenthesis.0986

In addition, each case will be indented one indent further in, or one indent deeper, than the switch statement.0992

You can see, all of these cases are indented one further.1001

And then, under each case statement, all of the statements that are part of the case statement group will also be indented one, relative to the case.1004

So, you see here, we have three different indent levels.1013

Finally, the closing brace, like our if and elseif and else statements, is going to be on a line all by itself,1017

and it is going to be aligned at the same indent level as the switch keyword.1023

The other thing we want to mention is that when we use a default case in our switch statements, we are always going to put it as the last case.1033

Now, technically you don't have to do that, but that is typically the convention, and it makes sense,1040

like with our ifelse statements--else is always at the bottom.1045

Well, with a switch statement, we are always going to put the default case at the bottom.1048

And additionally, we are always going to add a break statement to our default case.1051

Now, technically, because the default case will always be at the bottom, as we described, whenever its statements are done executing,1056

it is going to reach the end of the switch statement, and so it will stop executing,1063

because we know a switch statement executes everything from the case it matches until it reaches a break or end of statement.1067

That said, we are always going to include that break statement.1072

That would be for reasons...in case somebody decides to add an extra case at the bottom, below our default statement, or something like that.1076

It is just to improve the robust-ness of our code.1082

For the homework challenge for today, I want to have you create a script that accepts one GET variable named action.1088

We are going to let action take on one of three string values: one, two, or three,1095

which correlate to three different actions that a script might perform.1100

I want you to use the ternary operator, like you had used in our web application, to create a short variable called action1104

that gets set to the value that was supplied for action, if it was supplied.1112

It gets set to null if no value was provided.1116

Then, I want you to use a set of if, elseif, and else statements to do three things.1119

If no action was provided (which would be action is equal to null), then you provide an error message saying "no action was provided."1125

If the value is one to three, then I want you to output a separate message for each of those, saying "action1 will be performed";1135

"action2 will be performed"; "action3 will be performed."1141

If action is specified, but is has an action name that is not one, two, or three (for example, let's say the user specified action=four),1145

then I want you to output a message saying that some default action was performed instead.1152

Be sure to test these conditional statements that you create.1160

And you can do that by...you should use the name/value pairs name=one, name=two, and name=three 1165

to verify that you definitely get the output that says "action1/action2/action3 was performed."1171

as well as providing something where it says name=xxx, where xxx is not one, two, or three 1176

(in this case, as we just mentioned, we could say name=four).1182

The other thing is: you also want to not provide the variable name.1191

And actually, this is a little bit of a typo; these actually should be action=one, two, or three.1198

So, the other thing you should do is not provide the action variable at all, and you should output the message corresponding,1203

where it says "no action variable was provided."1211

Then, what I want you to do is go ahead and convert that if, elseif, else statement group all into one switch statement1215

that has a default case (and remember, the default case sort of corresponds to an else statement).1221

And then, test your switch statement with the same inputs you used up here in test 4, and make sure you get the same output, as required.1229

Be sure to format your elseif and your switch statements according to the course's coding conventions that we had talked about.1237

That ends today's lesson; thank you for watching Educator.com--I look forward to seeing you next time.1244