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!

Conditional Control Structures

  • Statements can be grouped together in statement groups so that the entire group of statements can be executed as if it were one statement.
  • Conditional control structures are used to conditionally execute statement groups when a required ‘test condition’ is met.
  • An if statement evaluates whether a test condition is TRUE , and if so, it executes a corresponding statement group.
  • An else statement is a conditional control structure associated with an if statement. When an if statement's test condition is FALSE, an associated else statement's statement group will get executed instead.
  • The isset() construct tests if a variable has been previously declared, and if so, that its value is not NULL . It accepts a variable as its argument and returns a boolean value.
  • Additional Resources:

Conditional Control Structures

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:26
    • Lesson Overview
  • Statement Groups 0:57
    • Statement Groups
    • Example
  • Conditional Control Structures 1:38
    • Conditional Control Structures
    • PHP Control Structures
  • if Statement 2:32
    • if Statement
  • if Statement (cont.) 3:49
    • if Statement Coding Example
  • else Statement 7:26
    • else Statement
    • if/else Statement Coding Example
  • isset() Construct 9:59
    • isset() Construct
    • isset() Construct Coding Example
  • Coding Conventions 15:13
    • Coding Conventions
  • Coding Conventions (Cont.) 16:39
    • Coding Conventions (Cont.)
  • Homework Challenge 17:25
    • Homework Challenge

Transcription: Conditional Control Structures

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

In today's lesson, we are going to be talking about what are known as conditional control structures--in particular, the if and else statements.0004

And it is a very exciting lecture, because for the first time, we are going to be able to make some decisions within our code,0011

based on user inputs and user actions.0016

So, we are going to be able to evaluate certain sections of code, based on things that happen from the user.0020

In particular, we are going to first talk about what is known as statement groups.0027

This is, as the name implies, grouping a bunch of statements together, and they get treated or evaluated as one.0031

We are going to talk about, obviously, the topic of the lesson, conditional control structures, 0038

and in particular the two control structures known as the if statement and the else statement.0043

We are also going to cover a language construct called isset, and then we are going to go over coding conventions as they apply to if and else statements.0048

What is a statement group? Well, essentially, as the name implies, a statement group is a bunch of statements0059

that are grouped together and sort of treated as one.0063

When you evaluate a statement group, you evaluate all of the statements within that group.0066

So, for example, in PHP, the way you denote a statement group is: let's say these two statements...you want them0071

to be executed as a group; you surround them by opening and closing curly braces.0077

What that does is: if this statement group right here is to get executed, that means that both of these echo statements will be executed.0085

So, both hello and world will be echoed out to your output.0092

Now, conditional control structures--what they provide is the ability to execute statement groups on a conditional basis.0099

And what they do is: they test a condition, and if the condition is met, then a particular statement group is executed.0105

For example, PHP provides a number of different control structures.0117

There is the if, the if/else statement (both of those we are going to be talking about today).0121

There is also the if/else/if control structure and the switch control structure, which we are going to be talking about in a couple upcoming lessons.0128

And then, these control structures down here are conditional control structures that allow you to loop over a statement group0138

and repeat it multiple times, based on certain test conditions.0145

And we will be getting those, as well, later on in the course.0149

The if control structure, commonly known as an if statement, is the most widely-used initial control structure.0153

And it is called an if statement (although technically, it is not a statement, as far as PHP goes, but that is the common vernacular0162

for referring to it)...basically, what the if statement does is: it tests the condition, which is a boolean expression.0168

And if the expression evaluates to true, then it executes a statement group associated with the if statement.0175

For example, this is the syntax that the if statement has; and what it has is the if keyword, 0181

and then a test condition immediately following that in parentheses.0187

And if the test condition equals true, then this statement group, 0192

which is all the statements enclosed in these opening and closing curly braces, will get executed.0199

If the condition is false, then they don't get executed at all.0204

One thing to note, also, is that (it's another terminology thing) one refers to executing statements within an if statement...0209

it is called being inside the if statement if this condition is true, and you are executing statements within the if statement's statement group.0219

That is just a terminology thing.0228

One other thing to note about if statements is that, if a statement group associated with an if statement0231

has only one statement in it (in other words, it's a statement group consisting of one statement), you don't 0239

actually have to include the curly braces around it, and the single statement can just be output as is.0244

Let's go and take a look at an example of the if statement in action.0251

Here we have a script called if.php, and it is going to run through a couple of examples of using the if statement.0258

Let's make it a little bit bigger.0268

In this first one, we used literal values as the test condition, and we say "if true, then go ahead and execute the statement group."0271

As we just learned, if the if statement's test condition is true, then the statement group gets executed.0281

Because true is always true, this statement right here will always be executed.0288

Now, if we try the same thing, but we put false as the test condition--false is always going to equal false, which is not true,0293

which means that this statement block in here is never going to get executed.0299

As another example of slightly different using of an expression, we could say, "If 0 is less than 1," and that is a boolean expression--0303

it's a comparison operation; 0 is always going to be less than 1, so this test condition will always evaluate to true.0311

And because of that, this echo statement within the if's statement group will always be executed.0318

Now, one other thing to point out is that these examples up here use literal values, or static values, within the test condition.0327

But more often than not, you are going to be using variables and combinations of variables--0335

in particular, variables using comparison operations--as part of the if condition.0340

In this case, we have a variable called boolean expression, just shortened to expr; we set it equal to true.0347

And then, if we run the if statement with the variable boolExpr as the test condition, what is going to happen is:0355

PHP is going to replace boolExpr with its value, which in this case is equal to true.0363

Because it's equal to true, this statement here is going to get executed.0368

As another example of things a little more complex, we could declare a variable called 1 and set it equal to the integer 1.0372

And then, we could put a comparison operation as the test condition (which is a boolean expression).0381

And we say "if 1, or the value of the variable 1, is equal to the value of 3-2--if that is true, then execute this statement here."0385

And because 3-2 is always equal to 1, this statement will get executed.0395

And down here, you can just see the results of these five different if statements that we had.0399

In the top one, because this was true, 'I will get executed' was printed out.0405

However, because this test condition was false, 'I will not get executed' does not get printed out.0409

And if we look down here, we see that is exactly what happened: 'I will get executed' was printed out, but the other one wasn't.0415

Because 0 < 1 is a true test condition, this is going to be echoed; and we can see that 0 is less than 1.0421

And then, for the final two if statements, because boolExpr in the first one equals true, and 1 is equal to 3-2,0429

both of their echo statements that are part of the if statement would get output.0438

One other control structure that we are going to introduce is known as an else statement.0448

And like the if statement, it is not actually technically a statement in PHP, but that is how it is commonly referred to.0451

It is a conditional control structure that gets associated with an if statement.0457

The way it works is that, if you have an if statement, and the if statement's test condition evaluates to false,0461

and there is an else control structure that follows the if control structure,0467

then whatever statements are inside the else statement's group get executed.0472

And so, for example, the if/else combination has this following syntax, where you have your if statement, 0479

as we had just described--it tests a condition.0487

If the condition is true, it evaluates this statement group; and then, it would proceed to the next line in the code.0491

However, if condition equals false, because there is an else control structure associated with it, then whatever statements0499

are in the else control structure statement group get executed.0510

So, every time this is false, these statements down here will get executed.0514

And as with the if statement, if an else statement group only contains one particular statement,0519

the curly braces around it can be omitted; they are optional.0525

Let's take a look at an example of the if/else control structure in action.0531

Here we have two simple if/else combinations.0540

In the first one, we have an if statement that says "if this condition is true, then we are going to echo this statement here."0544

"And if it is false, this statement gets echoed."0552

Because true (the literal value true) is always true, this statement will always get executed.0554

The else statement group will not be; and then, any code that would be next down the line would get executed after this statement was echoed.0560

Now, we switch things around, and we put the value false as the test condition, because if the test condition is going to equal false,0567

then the if statement group will not get executed.0575

However, there is an else statement group appended to the end of it, so the else statement group will get executed.0578

So here, it will say, 'Now I will get executed.'0584

And if we look at the output of these two if/else control structures, we can see 'I will get executed' was output from the first one,0586

and 'Now I will get executed' was output from the second one.0595

One other thing I want to talk about is something known as the isset construct.0602

This is something that is useful for particularly processing GET data to see if data was passed to the script that you needed.0608

For example, if you are expecting a name/value pair to a script by a GET, where the name is, for example, action,0616

and action is set equal to something like checkout, the isset construct can be used to see if action was passed into the script,0625

by checking to see if that key exists in the GET array.0638

And the way isset works (I'll get this out of the way here) is: it checks two things.0646

One it checks is if the variable that you pass to it has been previously declared, and also it checks that the value is not null.0655

The way it works is: it accepts a variable as its argument, and it returns a boolean value, true or false.0663

For example, if we declare a variable here, a, and we set it equal to 1, and then we run the isset construct on the variable a,0670

temp is going to be set to true; and the reason for that is because a has been both declared, and its value is not equal to null.0679

Now, if we went down here and we changed the value of a, so that it's equal to null,0687

and we run the isset construct on a again, temp is going to be equal to false.0692

And that is because, even though a was declared, its value was null.0698

And then, finally, if we run isset on a variable b that has not been declared (let's assume this is all one script),0702

because b has not been previously declared in this script, isset is going to return false.0710

Let's take a look at what that looks like, again, in action.0717

We have a script called isset.php, and what it does is: because isset returns a boolean value, we can use it within an if test condition.0722

So, this is saying if the variable name was passed to the script by a GET (which means that the _GET array would contain0732

a key/value pair with the key name), if that is set, then we are going to echo these statements here.0746

It says a name was provided by GET, and it is going to output the name.0753

On the other hand, if it wasn't provided, then it is going to use the else statement to output and say the name was not provided by GET.0757

And if you look up here at the top of our script, we can see, we just have isset.php with no GET query string appended to it.0765

So, because there is no name as part of a GET query, isset on checking this variable name is going to return false;0772

it is going to echo "'name' was NOT provided via GET."0786

Now, if we were to go up here and add name (let's say I put my first name in here, Matthew), into the script,0790

now isset is going to return true, because the GET variable name exists.0798

And so, it is going to evaluate the if statement group; so it's going to say, "'name' WAS provided via GET" "The 'name' provided was 'Matthew.'"0803

And one other thing to do, just as a refresher on going over how URL's are encoded--if we remember, spaces in URL's0812

are encoded with a + sign, so if I want to output my first and last name here, I can do Matthew+Machaj.0820

And if I do that, you can see that Matthew Machaj was output.0828

And that, again, is just sort of a review of URL encoding.0830

And the reason that the isset is useful, like I said, is for checking GET parameters, because if you try to access0835

a GET value that was not provided to the script, you will get a warning.0841

And also, if you try to use GET values that weren't passed in, your script is not going to work as executed.0847

For example, we look at the code of isset, and if I were to go in there, and after the else statement, let's say0854

I go ahead and try to echo (move this over a little bit) a GET value, or a GET variable that doesn't exist.0864

Let's call it lastName, and if I try to echo that, because it is not passed into the script, 0882

if we go back and use the same query string as before, we are going to get a warning.0889

And if we look down here and refresh it, it's going to say, notice: undefined index lastName,0894

which says that the key lastName doesn't exist in the GET array, and that is because we didn't provide it.0899

That is one of the main uses of the isset construct--to be able to check if GET data were provided.0905

Now, I want to talk about coding conventions as they apply to if statements and else statements.0914

Basically, the examples we have seen are formatted the way that we are going to be formatting them in class.0921

Namely, first, there will always be a space between the if keyword and the opening parentheses of the test condition.0927

And additionally, after the closing parentheses of the test condition, there will be a space, followed by the opening curly brace.0933

So, the curly brace for an if statement is always going to be on the same line as the if keyword and the if condition, and it is going to be one space after.0940

Similarly, for an else statement, the opening curly brace for an else statement group is going to be on the same line as the else keyword.0948

It is going to be separated by a space.0958

Additionally, any statements within an if/else statement group are going to be indented one level deeper than the if keyword.0962

When we go back, and we look at our example here--in this isset example, we can see that all the statements 0970

within the if statement group and the else statement group are one indent deeper than the if or else keywords.0976

The other thing to note, as you can see in the script, is that the closing brace for an if or else statement group0987

will always be on its own line, and it will be indented the same level as the if or else keyword.0993

And as far as the else statement goes, the else keyword will be on a new line, following its corresponding if statement's closing brace.1002

So, for example, if we look back here, we can see the closing brace for this else's corresponding if statement is right here.1012

So, else is on the following line, as opposed to having else up here, which sometimes you see in different people's coding style.1018

But for this course, we are going to be doing it like this.1026

And then, finally, even though statement blocks, if they contain one statement, don't have to have curly braces surrounding them,1029

as we had mentioned, we are always going to surround our statement groups for if and else statements with curly braces,1035

even if there is only one statement.1042

So now, I just want to talk about the homework challenge for this lesson.1047

I want you to create a script that contains a single if/else control structure.1050

And what it is going to do is: it is going to test, using the isset function, if the variable _GET ID is set.1055

It checks if the GET parameter ID was supplied.1066

If it was--if that is true that it was supplied--you should output the ID passed in.1069

And if it is not true, you should output an error message stating that you need to provide an ID to the script.1077

And what that is going to do is: you should be able to make use of an if/else statement in order to do that,1087

similar to what we had done in the isset.php example in here.1091

One thing I want to say is that I strongly encourage you to not cut and paste code from the lesson examples--1095

that you should practice typing these if/else statements on your own.1102

And the reason for that is: there are often a lot of mistakes involved with if/else statements and control structures in general,1105

where you leave out a parentheses or a curly brace, and so the only way to really get practice with that,1111

and to learn where the mistakes are made and how to correct them, is to get in there and type it on your own.1116

And also, I just want to mention that you should follow (when you write your sample script or homework script) 1122

the coding conventions we just outlined for the if/else statements.1129

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