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

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!

Logical & Ternary Operators

  • Logical operators are used to create complex boolean expressions and operate on boolean operands. PHP’s logical operators are:
    • NOT ( ! ) – unary operator that returns TRUE if its operand is FALSE
    • OR ( || , or ) – binary operators that return TRUE if either operands are TRUE
    • AND ( && , and ) – binary operators that return TRUE only if both operands are TRUE
    • XOR ( xor ) – binary operator that returns TRUE if either operand is TRUE , but not both
  • The OR and AND logical operators have two options because the options have different precedence:
    • || has a higher precedence than or
    • && has a higher precedence than and
  • The OR and AND logical operators are known as short-circuit operators meaning that if the value of their left operand can determine the result of the operation, the right operand never gets evaluated.
  • The ternary operator is the only ‘ternary’, or three-operand, operator in PHP. If a ternary operator’s 1st operand evaluates to TRUE , then the value of the 2nd operand is the result of the operation, otherwise the value of the 3rd operand is the result of the operation.
  • Additional Resources:

Logical & Ternary Operators

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:17
    • Lesson Overview
  • Logical Operators 0:49
    • Logical Operators Definition
    • NOT (!)
    • OR ( ||, or)
    • AND (&&, and)
    • XOR (xor)
  • Logical Operators (cont.) 2:54
    • The OR and AND Logical Operators
    • Precedence of Logical Operators
  • Logical Operators Coding Example 3:58
    • Logical Operators Coding Example
  • Short-Circuit Operators 9:54
    • Short-Circuit Operators
    • Coding Example
  • Ternary Operator 14:07
    • Ternary Operator
    • Syntax and Example
  • Coding Conventions 17:36
    • Coding Conventions
  • Homework Challenge 19:08
    • Homework Challenge
  • Homework Challenge (cont.) 20:26
    • Homework Challenge (cont.)

Transcription: Logical & Ternary Operators

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

In today's lesson, we are going to be introducing a bunch of new operators.0004

In particular, we are going to be introducing a group of operators known as the logical operators.0008

And we are also going to be introducing what is known as the ternary operator.0012

So, as mentioned, we are going to be talking about a number of different operators that fall under the class of logical operators0019

which allow you to create complex boolean test conditions, which are often used in if statements and so forth.0025

We are going to talk about how a couple of the logical operators are known as short-circuit operators, and we are going to explain what that means.0034

And then, finally, we are going to end up by talking about the ternary operator, which is a special operator in PHP.0041

As mentioned, logical operators are used to create complex boolean expressions, and they operate on boolean values.0051

The ones that are provided by PHP are the NOT operator, the OR operator, the AND operator, and the XOR operator.0060

What they do is: for example, the NOT operator is a unary operator.0070

And what that does is: whatever boolean value it acts on, it returns the opposite.0074

So, if the boolean value is true...if you prepend the NOT operator, which is the exclamation point, to a boolean value, 0079

it's going to return the opposite, so if you prepend it to true, it's going to return false.0088

If you prepend it to false, it is going to return true.0092

Then, there is also a binary operator known as the OR operator; it has two forms.0097

It can be used with two vertical bars, which are the same key on the keyboard as the backslash, typically.0103

And then, there is also the operator which is the or keyword.0109

Now, this, again, is a binary operator, and what it does is: it returns true if either its left or right operand are true.0114

So, it operates on two boolean values; so the only case it returns false is if both of its operands are false.0122

AND is the converse of the OR operation, and it also has two ways to specify that: double ampersands, and then also by the and keyword.0129

And it is a binary operator that returns true only if both its left and its right operand are true.0141

So, if they are not both true, every other condition returns false.0146

Finally, there is the XOR operator, which is known as the exclusive OR operator; and this is how it is specified in the code.0151

And it is a binary operator that is similar to the OR operator; it returns true if either one of its operands is true, but not if they are both true.0160

So, it's an exclusive OR: it can only be one or the other.0170

The OR and AND logical operators, as mentioned, have two different options.0176

The reason they have the different options (for example, the OR operator has the vertical bar option and the or option)0180

is because they have different operator precedences.0189

For example, the vertical bars have a higher precedence than the or operator, 0194

and the double ampersands have a higher precedence than the and operator.0199

The lower precedence versions are not used very often, and we are not really going to be talking about them much in this course.0204

The ones you will see will mainly be these double vertical bars, the or operator, and the double ampersand.0210

Now, just to talk, in general, about the precedence of all of the logical operators, from highest to lowest, they go this way.0216

Basically, the NOT operator has the highest precedence; then, it's the AND, the OR, and so forth.0223

And additionally, all the logical operators are left-associative, except for the NOT operator, the exclamation point, which is right-associative.0231

Let's go and take a look at a script called logicalOps.php that demonstrates these logical operators in action.0239

We are first going to talk about the NOT logical operator, which is the exclamation point.0248

And as we know, with if statements, an if statement evaluates a statement group if its test condition is true.0254

In this case, we have the NOT operator being prepended to the literal value false.0262

So, as we mentioned, it says the opposite truth value of what it is provided--of its operand.0269

Since it is not false, the opposite of false is true--this test condition here is going to evaluate as true.0275

So, this echo statement right here will be output.0281

Now, if we do the same thing using a boolean variable to show that it can work on variables, as well,0286

we can create a temp boolean variable, and we set it equal to true.0292

And then, we set the condition equal to !$tmpBool; so in this case, it's going to be "if not true."0295

And "not true" is always going to be equal to false; because this test condition will be false, this will not get executed.0302

And instead the else statement group will get executed, so this statement down here will be executed.0309

Now, to demonstrate the OR operator: as mentioned, the OR operator will return true any time either one of its operands is true.0315

So, using literal values, in these first three cases here, where we have either one of the operands as true, 0326

or in this case (the third case) both of the operands are true, each of these test conditions are going to evaluate to the expression true.0333

So, all of these echo statements within these if statements are going to get output.0342

Now, the only time the OR operator returns false is if both of its operands are false.0347

So here, if we have if false OR false, it's going to return false, and so, the else echo statement is going to be echoed.0352

Down here, this is the same exact thing with the same exact results, except it uses the lower precedence or operator.0361

But it demonstrates the same logical functionality, which is: if one or the other is true, return true.0367

For the AND operators, as mentioned, that binary operator only returns true if both of its operands are true.0374

So, in this first case here, if we say true AND true, that will evaluate to TRUE, and so this echo statement will be output.0382

In these other three examples, where we have true and false, false and true, or false and false--0389

all of those are going to return the boolean value false, because both operands are not true.0394

And so, in these cases, the echo statements in the corresponding else statement groups are what is going to be executed.0400

Down here is just, again, an example of the lower-precedence and operator.0409

It has the same logical functionality, only returning true if both operands are true; and this is just what it looks like in code.0415

Now, the XOR operator, which only has one version, returns true if either one of its operands is true, but not both.0423

So, in this case--these first two cases here, where only one of the operands is true--these if echo statements will get output.0433

However, in each of these cases, where it's true XOR true and false XOR false, both of those logical operations will return false.0441

So, the else echo statement will be output.0450

And down here, it just shows the results of these different operations.0455

Basically, it is the output from all of these if and else statements that we had up above, and basically saying...0460

It's like a truth table, explaining how these operators work--not false equals true; not true equals false; and so forth.0466

The OR operators return true, except when both operands are false.0472

The AND operators only return true when both of its operands are true.0478

And then, as we learned with the XOR operator, it only returns true when one of its operands is true.0483

Now, if we look at the code behind this (this is logicalOps.php--let's scroll down to the Code section)...for example, where we have0491

the OR operator, in the fourth demonstration of the vertical bar OR operator, we have an if/else statement.0504

It is saying, "If false OR false, echo this statement here; otherwise, echo this statement here."0512

Well, because false OR false is always going to be false, this statement here is never going to get echoed.0519

So, what we can do instead is use a combination of logical operators.0526

So, we can actually change this and get rid of the else statement by including the NOT operator.0529

And what we are saying is: if false OR false is not true (meaning it's false), then go ahead and evaluate the if statement.0537

So, in that case, we can get rid of this else statement and replace its output up here.0546

And this shows, again, a combination of using logical operators in combination with each other.0554

So, we have the first logical operation, which is false OR false, which is going to return the value false.0559

Then, we are performing the NOT operation on that false value, which is going to return true.0564

Therefore, this echo statement is going to return, which says false OR false equals false.0568

If we save this and go back and look at the page, down here in the results for the OR operator, we should see the same results.0574

And we refresh, and you can see that false OR false equals false.0586

That is an example of using a combination of logical operators.0589

Now, two of the operators, the OR and the AND logical operators, both versions of those operators, are known as short-circuit operators.0595

What that means is: if the value of their left operand, which is an expression, can determine the output of their operation,0602

then the right operand, or right expression, is not evaluated at all.0610

For example, with the OR operator, we know that it returns true any time either operand is false.0615

So, if the first operand--left operand--evaluates to true, there is no need to evaluate the right operand, 0621

because we know that the operation is always going to return false.0627

Likewise--or in the opposite way--for the AND operator, the AND operator, we know, only returns true when both of its values are true.0631

So, if the left operand, or first operand, is false, we know the AND operation is always going to return false.0638

And so, there is no need to evaluate the right operand or right expression.0644

So, if we go and look at a script called shortCircuitOps.php, we can see this short-circuit operation in action.0649

For the OR operation (and we are going to use the vertical bar version), if we create a variable called orTemp and set it equal to 0,0662

and then, we say, if true, orTemp=1, orTemp=1 is an assignment statement, and assignment statements, as we know,0671

are expressions, and they have a value; and the value of an assignment statement is the value that is being assigned.0681

So, this expression, if it were to be evaluated, would have the value 1.0687

So, if we go ahead and run this if statement here, because this first operand is true, that determines the output of the operation.0692

The OR operator is a short-circuit operator, so this second expression never gets evaluated.0700

The value of orTemp never gets updated to 1, so when we echo this statement down here that echoes the value orTemp,0705

it is still going to have the value of 0.0711

And if we look down at the bottom, we can see: orTemp equals 0 in this first example.0713

Now, let's say we switched the order around: the left operand always gets evaluated.0718

In this case, this is going to evaluate orTemp=1, so that means orTemp is now going to be updated--it is going to have the value of 1.0722

And then, it is going to say 1 OR true; and since that is always going to return true, then what you have is:0729

we are going to echo out the orTemp value, and we are going to see that it is actually going to update it, in this case.0736

So, if we look down here, we see that orTemp=1.0740

That shows how the short-circuit operation of the OR operator works.0743

Similarly, for the AND operator, if we declare a variable andTemp, and we set it equal to 0, and we have a logical operation0748

that is false, "AND"-ed with the assignment operation where you are assigning the value 1 to andTemp,0757

we can see that, as a result, andTemp is never going to get set equal to 1.0765

When this echo statement occurs, andTemp is going to output 0.0770

That is because we know that the left operand is false, so the AND operation is always going to return false.0773

There is no need to evaluate this left expression.0780

And as you can see, here we used what we had talked about in the last example, where we were combining logical operators.0784

Here we have an AND operation, false AND this assignment statement.0792

And if that returns false (which it is going to), then we can use the NOT operator; prepend that to that, and it's going to 0796

return it as true, which makes this echo statement evaluate.0804

If we look at the output of this if statement right here, we can see that andTemp is going to be equal to 0.0809

That is because this andTemp=1 assignment statement never occurs.0814

Again, as we saw with the OR example, if we switch the order of the operands on the AND statement,0818

this andTemp=1 assignment statement will always get executed, so the value of andTemp is going to get set to 1.0824

And because this operation is going to return false, and then we perform the NOT operation on that, which is going to return true,0833

this echo statement will occur; andTemp will be outputted; and here, we can see that it is output to the value of 1.0839

Now, we are going to talk about a special operator called the ternary operator.0848

Sometimes, it is also referred to as the conditional operator.0852

And as the name implies, it's a ternary operator, which is a 3-operand operator; and it is the only ternary operator within PHP.0856

And the way it works is (here is an example of what it looks like in action, down here): 0865

its first operand here, op1, is interpreted as a boolean expression, or a boolean value.0869

If it equals true, then (after the question mark is the second operand) the second operand will be returned.0877

So, whenever op1 equals true, this will be returned as the output of this operation.0885

If op1 equals false, then op3 will be returned.0895

And again, just to mention, it uses a question mark to separate the first and second operands, and the colon to separate the second and third.0905

So again, if op1 is true, the second operand is the output of the ternary operation.0913

If op1 is false, the third operand is the output of the ternary operation.0918

Let's take a look at a script that demonstrates that, ternaryOp.php.0925

Basically, what we have here is a ternary operation where we have a variable result, and we are setting it equal to the output of a ternary operation.0931

And here, we can see, a ternary operation has the question mark, the colon, and the three operands.0940

We set the first operand to the literal value true; so because it's true, we know from what we just learned0946

that the ternary operator outputs the value of the second operand.0953

So, in this case, the result is going to be set to the second op; and when we output the result, it is going to say secondOp.0956

And if we look down here, we can see: this first output is output=secondOp.0962

Likewise, if we instead set result equal to this ternary operation, because the first operand is false, 0967

the output of the ternary operator is going to be the third operand; in this case, it's the string thirdOp, so our output is going to say the string thirdOp.0974

And in fact, that is what it does.0983

Now, one other thing you can do is: this uses literals, but to demonstrate--you are typically not going to be using literals0987

as the first operand in a ternary operator; you are going to be using variables and combinations of variables,0993

using comparison operators, logical operators, and so forth.1000

Here, if we declare a variable, temp, equal to 1, and then we run the comparison operation temp=1 1003

(and this is, again, as we learned from our lesson on comparison operators, the equals operator),1010

and it returns true with both values on each side of it, both of its operands' values are equal.1014

So, temp=1, so 1=1=true, so it is going to output the second operand, which says "temp=1."1020

And we can see down here, outputted, it says "temp=1."1026

Now, if instead we go ahead and set temp equal to 0 and run the same ternary operation and store its output in result,1030

this is going to evaluate 0==1, which is going to evaluate to false, because 0 is not equal to 1.1038

So, the third operand is going to be output, which in this case is a string that says temp not equal to 1.1045

And we can see our output down here; it says temp not equal to 1.1051

I just want to quickly talk about coding conventions as it relates to these different operators, particularly the logical operators.1057

Basically, PHP is always going to abide by the rules of precedence and associativity as it evaluates different expressions.1065

For example, this if test condition here and this if test condition right here actually evaluate to the exact same boolean value.1071

It is a combination of two logical operators--a logical AND and a logical OR operator.1083

However, this one has parentheses around the AND operation.1090

Now, the AND operation has a higher precedence, so when this statement up here gets evaluated, this operation will always occur first.1093

The result of that will be "OR"-ed with the a operation.1103

Now, down here, the same thing happens, except we explicitly put in these parentheses.1106

What that does is: even though the parentheses aren't needed, it makes the code a little bit easier to read and a little bit easier to understand.1111

So maybe, if somebody is reading your code that is not as experienced in PHP and in working on it,1118

they can intuitively know that, "OK, this AND operation is always going to happen."1123

And it goes along the lines of: if you can make your life easier by adding just a few extra things,1126

then why not take the extra step to go ahead and add these extra parentheses.1131

So, in our examples in this course, we are always going to add extra parentheses to make it explicitly clear1135

which order the operation is going to occur in, even though it's not necessarily necessary.1142

For today's homework challenge, I'm going to have you create a script that accepts two GET variables--one called sex and one called age.1149

For this challenge, I want for the sex variable to either take on one of two values: male or female.1158

And then, let age represent the age of a user, and have it take on the values between 1 and 99.1164

And I want you to add a single if statement to your script; and this is to practice using the NOT operator.1171

And it is going to test if the sex GET variable is not equal to male; then, it should output a message that says, "The user's sex is female."1177

And what you can do to test this out is to manually edit GET query strings, using both of these name/value pairs, sex=male and sex=female.1187

Make sure you are only getting your output message if the sex is listed as female.1196

Now, I want you to alter the if statement, so that, instead of having its NOT operation, it is going to have an OR operation.1202

And what I want it to do is output true any time the age provided is less than 25, or if it's greater than 50.1209

And if that condition is met, you should output a message that says, "The user's age is not in the 25-50 age range."1219

The way you can test that is to generate test queries where you set age equal to three different values, 1228

to make sure that the test condition is completely evaluated.1234

You set it once equal to a value less than 25, once equal to a value greater than 50, and once to a value in between 25 and 50.1239

And when you run these test queries, the only time that you should see output is when the user's age is not in the 25-50 range.1248

Go ahead and test that and make sure that it is running correctly.1257

Then, I'm going to have you expand your test condition even further, and this is to get practice with using combinations of logical operators.1261

We are going to include an AND operator; and I want the test condition to check if the user is not between 25 and 50, and they are also a male.1269

So, it is going to output a message, if this condition is met, that says, "The user is a male not in the 25-50 age range."1279

And the way you can test that is to use the same query strings that you used in step 5 up here,1289

with a different age provided to test the three different age groups.1294

But then add the name/value pair sex, once with the value male and once with the value female.1300

And you should only see the output when the sex is equal to male.1307

And so, that is going to give you practice working with a combination of logical operators--in this case, the AND and the OR operator.1311

Again, as we had mentioned, the AND always has higher precedence than the OR operator.1316

But practice, when you write your code, using extra parentheses in there to make it explicit to anybody that looks at your code1321

exactly what order the operations are occurring in.1327

Then, I want you to just go ahead and remove the if statement from the script.1332

And we are going to get a little practice with the ternary operator.1335

I want you to create a variable that is an output of a ternary operation, and the ternary operation...1338

the first operand is going to be a test to see if the sex is female.1343

If the sex variable provided by GET is female, it is going to set the variable value to "the user is female."1348

If it's not, it is going to set the variable value to "the user is male."1357

And when you do that, remember that, if the test condition of a ternary operator--the first operand is true, the second operand is output.1361

And if it is false, the third operand is output.1368

The way you can test this is by generating two query strings, one with sex=female and one with sex=male.1371

And what you can do is run this script with both of these query strings, and make sure that you get the correct message output.1378

When you provide sex=male, you get an output that says "the user is male."1388

When it says sex=female, you get the output that says "the user is female."1392

That ends today's lesson; thank you for watching look forward to seeing you next time.1397