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!

Boolean & Null Data Types

  • The boolean data type is a scalar data type used to represent a truth value: either true or false.
  • There are only two boolean literals: the special keywords TRUE and FALSE .
  • Comparison operators are binary operators used to compare two expressions. The result of a comparison operation is a boolean value.
  • There are several comparison operators:
    • Equal: (==)
    • Not Equal: (!=)
    • Greater Than: (>)
    • Less Than: (<)
    • Greater Than or Equal to: (>=)
    • Less Than or Equal to: (<=)
    • Identical: (===)
    • Not Identical: (!==)
  • The identical and not identical operators compare BOTH the value and data type of their operands. All of the the operators compare their operands based on value only.
  • The null data type is a special data type that is used for variables that have no data value assigned to them.
  • There is one null literal, which is the special keyword NULL .
  • A variable can only be of the null data type if it has been specifically assigned the literal NULL , or if:
    • the variable has not yet been assigned a value, or
    • if the variable has been ‘unset’ using the unset() construct
  • Additional Resources:

Boolean & Null Data Types

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:12
    • Lesson Overview
  • Boolean Data Type 0:38
    • Boolean Data Type
    • Two Boolean Literals
  • Boolean Example 1:50
    • Boolean Example
  • Comparison Operators 4:00
    • Comparison Operators Definition
    • Common Comparison Operators
    • Comparison Operators Example
  • Comparison Operators (Cont.) 10:10
    • Identical and Not Identical
    • Example: Identical and Not Identical
  • Null Data Type 13:36
    • Null Data Type Definition
    • Null Literal
    • Variable and Null Data Type
    • '==' Operator
    • Null Data Type Example
  • Coding Convention 18:41
    • Coding Convention
  • Homework Challenge 19:17
    • Homework Challenge

Transcription: Boolean & Null Data Types

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

In today's lesson, we are going to be talking about two new data types 0004

that we haven't talked about before, the boolean data type and the null data type.0007

As part of talking about the boolean data type and the null data type, we are also going to go over what are known as comparison operators.0014

We are going to be introducing a new class of operators; previously, we have worked with arithmetic operators0021

and the concatenation operator; we had introduced those a few lessons ago.0026

We are going to learn a few more operators today, and then we are also going to briefly talk about 0030

coding conventions, as it relates to these two data types.0034

What is a boolean data type? Well, a boolean data type is one of the four scalar data types in PHP.0040

We have worked with three of them already: the integer, the float, and the string.0046

The boolean is the fourth scalar data type, which is a data type for representing single values.0050

A boolean is used to represent a truth value, meaning it's either true or false--it's one or the other.0057

Booleans, or bools, as they are commonly referred to, 0063

are used extensively throughout our code, and we are going to be using them, from this lesson onward, all the time.0068

They are the output of comparison operations; I mentioned a minute ago, we are going to be learning about the comparison operators in this lesson.0073

They are also the output of many functions, and so we are going to be dealing with these boolean values all the time.0080

In PHP, there are two boolean literals that you can assign to a boolean variable, and they are the special key words true and false.0086

True and false are treated in a case-insensitive manner by PHP, so you can do them in any case that you want to.0098

But typically, the convention is to do them either all uppercase or all lowercase, like this.0105

Let's take a look at a sample PHP file called booleans.php that demonstrates the use of boolean variables.0111

The first thing we are going to do is do a var_dump function on both of the boolean literals: the boolean literal true and the boolean literal false.0122

That is going to show you 1) how to specify the boolean literals (which is--you just write them in the code in all capital or lowercase letters--0131

just as is, and the PHP Interpreter knows to interpret them and knows they are not strings or anything like that--they are special,0141

reserved words), and var_dump, as we know--it outputs the type and the value of whatever piece of data is passed to it.0147

So, when we var_dump on the true value and the false value, we should see that it's a boolean, and we should see that they're equal to true and false.0156

So, if we go and load this page up (we can't go to this part down here--we haven't gotten there yet), you can see,0165

the true literal is...and that literal is a bool data type, as we expected, and its value is true, as expected.0172

At the same time, the false literal is interpreted as a boolean, and it's the false value, just as expected.0178

Now, you can also (as we are going to be commonly doing) assign boolean literals to variables.0188

And then, you can assign a variable that has a boolean value to another variable.0194

For example, here we have three variables we have created.0199

One is called testBool1; we set it to the boolean literal value true.0203

We have testBool2, which we set to the boolean literal value false.0208

And then, we have created one called assignBool, where we have just done an assignment operation,0212

which is saying "Assign the value of testBool1 to assignBool," so it's going to be true.0217

And then, we just use the var_dump function on all of these different variables.0223

And if we go and look at the output, you can see, testBool1 is a boolean data type; it's true.0228

testBool2 is a boolean data type--it is false; and assignBool is a boolean data type of value true.0233

So, comparison operators: what are they?0243

Well, as the name implies, they are used for comparing two different data values.0247

They are binary operators, and because they are binary operators, as we know with binary operators, they operate on two expressions.0252

And as an operator, it takes expressions as input, and it generates expressions as output.0260

The output of a comparison operation is a boolean value, and what it does is compares the values.0266

Depending on the different comparison operators we are going to talk about, if the comparison is valid, it outputs true; if it's not, it outputs false.0273

Now, PHP has several common comparison operators: there is the equal operator, which is two equal signs, not one.0281

The single equal sign, as mentioned down here, is the assignment operator, which you have worked at before.0289

The equal comparison operator, which compares the value of two variables, is not the same.0295

It is two equal signs: and that is sometimes a common problem with new programmers to PHP--getting mixed up with using one equal sign versus two.0300

One equal sign is an assignment operator; two equal signs is the equal comparison operator.0311

I'm going to see what that looks like in code in a minute, and what that is for doing is to compare if the two operands of that operator are equal or not.0317

There is also the not equal operator, which is an exclamation point, followed by an equal sign.0326

There is an optional convention to also use the less than and greater than symbols.0333

It is not as commonly used, and in this course we are going to be using this one exclusively.0339

What the not equal operator does is compares the two operands that it is given.0343

If they are not equal, it outputs true; if they are equal, it outputs false.0348

Then we have the greater than and less than comparison operators, and what those do is:0353

they return true, for example, if the operand on the left is greater than the one on the right--it returns true; if it's not, it returns false.0358

And then, there are two other versions of each of those--the greater than or equal to or less than or equal to,0367

which does the same sort of comparison, but allows the value to be equal, as well.0373

So, you could say "2 is greater than or equal to 2"; the answer would be true, because it's not greater than, but it's equal to 2.0378

And the greater than or equal to operator looks like this, and the less than or equal to operator looks like that.0386

If you remember, back from math, this is how it used to be written--greater than or equal to, and then the less than or equal to is written like this.0394

And so, the equivalents of these are these operators here.0404

Let's take a look at...we have some code here that demonstrates all of the different operators.0410

I have gone through and...all of the different operators we have just mentioned--shown some different examples of how they work.0418

And rather than looking at the code, we will look at what the output of it looks like.0425

Here we see the output of some comparison operations.0433

The equal operation--the way it works is: you have one operand--one value; you have the equal comparison operator; and then you have the other operand.0437

What it does is: it compares those two values; if they are equal, it returns true.0452

It says, in this case, 1 is equal to 1--that is true; 1 is equal to 2--that is not equal, so it returns false.0456

You can do it the same for all the different data types; so for the float--for example, 1.0--is that equal to 1.0? True.0465

But 1.0 is not equal to 2.0.0472

So, as you can see from these comparison operations, they make a comparison, and then they output a boolean value as a result.0475

You can see some string comparisons here.0482

One thing you can note is that the comparison operation for strings is case-sensitive.0485

So, here we have hello; we are comparing it to the other string, hello, and that is going to return true, because they are the same.0490

But down here, we have hello that is in all lowercase, and then we have Hello with the first letter capitalized.0498

And as you can see, that is going to return the value of false; the string has to be exactly the same, character-for-character.0504

You can also compare boolean values; so in this case, we are comparing the boolean literals we have just learned about.0511

Is true equal to true? Yes, so the result of that comparison is true.0517

Is true equal to false? No; the output is false.0521

Now, the not equal operator is just the opposite of the equal operator.0524

If the two values it is comparing are not equal, it returns true; if they are equal, it returns false.0528

So basically, all of the examples that we had up here--in the not equal operator, they have the opposite result.0533

And you can see, if you look down, every result is the opposite one.0540

For example, true is not equal to true--that is false; they are not equal.0543

1.0 is not equal to 1.0--that is false, because they are equal.0547

Now let's move on to the greater than or greater than or equal to comparison operators.0553

We can see how they work: basically, it's an operand, and the operator, and the right operand.0560

And 2 is greater than 1 is true; 2 is greater than 3 is false; 0566

and because this is not a greater than or equal to operator, 2>2 is also going to return false, because it is not greater than 2.0571

Down here, we have done these same examples, but we have used the greater than or equal to operator.0578

So, 2 is greater than or equal to 1 is still going to return true; 2 is greater than or equal to 3 is going to return false.0582

But 2 is greater than or equal to 2 is going to return true, and that is because of the additional constraint of it being equal.0588

And then, the same thing applies down here for the less than or equal to.0597

The same exact operations, but just in reverse--it's a less than or a less than or equal to operation.0601

Now, PHP also has two other comparison operators, known as the identical and not identical operator.0611

All the other comparison operators we had talked about compare the values of the two operands.0620

It compares the value on the left to the value on the right, and if they're the same, it outputs true or false, depending on what the comparison is.0624

The identical ones are similar to the equal and not equal operator, except not only do they compare the values of the two operands,0632

but they also compare their data types.0639

So, not only do the values have to be the same--they have to be, for example the number 1; 0641

for 1 to be equal to 1, they both have to be the same number, but they also need to be the same type.0646

So, a 1 that is represented as an integer and the number 1 that is represented as a float are not going to be the same.0650

Not identical is the opposite of that...0659

Well, first of all, identical is represented by three equal signs; that is the identical comparison operator.0664

The not identical comparison operator is an exclamation point, followed by two equal signs.0669

And basically, what the not identical one does is: it compares the two values and, if they are not identical,0675

meaning if they are not the same value and they are not the same type, it is going to return false.0680

For example, down here we have a variable a, and we are setting it equal to 1, the numeric value 1.0686

We have a float that also is the number 1 (so numerically, they are equal).0693

So, if we come down here to this first comparison, and we use just the regular equal comparison operator from before,0698

that just evaluates the numbers based on their values.0706

Since they both have the same numeric value, 1, this is going to return true.0709

However, if we use the identical operator, which is three equal signs, it is going to return false, because,0715

even though they have the same numeric value, they have different data types.0722

Here, they were both a value of 1 numerically, but one was an integer and one was a float.0728

Let's look at some examples of that, as well.0734

On the same script, we have the identical and non-identical operators down here.0737

As you can see from before, the integer 1, testing whether it's equal to 1.0--it returns true, because they are both numerically 1.0743

The values are both the same.0751

Here, we use the identical operator; it compares 1--is it identical to 1?0753

The answer is true, because they are both integers, and they are both equal to a value of 1.0757

Down here, we compare the integer 1 to the float 1.0, and as we mentioned, that is going to return false0762

because, even though they have the same numeric value, they are of different data types.0769

And not identical works in the opposite way.0774

So, for example, we have the not equal operator--1 is not equal to 1; that is false, because their values are the same.0777

And we could say 1 is not equal to 1.0, and that will return false, because the numeric values are the same, so they are equal, so not equal returns false.0785

However, if we do 1 is not identical to 1.0, it is going to return true, 0794

because in order for that to return false, they have to have the same value and be of the same data type.0799

In this case, they have the same value, but they are not the same data type, so they are not identical; it is going to return true.0809

Now, we are going to talk about the null data type; and null is a special data type--it doesn't fall into the scalar or composite data type categories.0818

And basically, what the null data type is used for, is for variables that have no value.0825

Now, a variable always has to have a data type to be used, whether it's an integer, a float, an array...we just learned about the boolean...0830

But if there is no value assigned to a variable, the absence of a value is considered its own data type.0839

That data type is null, which is the absence of a value.0845

There is one null literal to assign; if you want to assign a value, make a variable equal to null, or of the null data type.0850

You assign it the special key word null; and again, like true and false, this is case-insensitive, how it is interpreted by PHP.0859

And the convention, again, is to use either all uppercase or all lowercase letters.0868

And so, the way you can have a variable be equal to null (meaning it has no data type) is: you can specifically assign it the literal value null.0872

Or, two other options: you can create the variable and not assign a value to it...0882

For example, you could just have a statement that is a followed by a semicolon; assuming we have never declared a before in our script,0888

a is going to have the value of null, because you haven't set a value to it.0898

The other thing is: if we have a variable that has already had a value, and we want to make it null--we want to have it so it has no value--0901

we can use the unset construct, which looks like this.0907

And what you do is: you pass the variable that you want to set to null into it, and then, that variable is now no longer set, and it's equal to null.0912

Now that we have learned about comparison operators, we can make use of the equality operator.0925

A common operation is to compare variables to null; that is like, say, we have a variable num1, 0930

and we want to see whether it is associated with a number yet--if it has been initialized.0937

We can say "num1--is it equal to null?" using the equal to operator.0941

If it is equal to null, that is going to return true, and if it's not, it is going to return false.0951

And so, that is a common comparison we are going to do.0956

Let's take a look at a script that does that.0958

We have a script called null.php, and here we have three different variables.0963

We have a variable called val1, and here we explicitly set it to NULL--the NULL literal--an all-capitalized version, which we are going to use in this course.0971

And that means that val1 is a null data type; it has no value associated with it.0979

The other thing that we can do to get a variable to be a null data type is just declare it, but not initialize it.0986

So, here we have declared val2, a new variable that hasn't been used in the script before.0992

But we didn't assign it a value; therefore, its value is null.0997

The other thing that we can do to give a variable the null data type is to unset it.1000

Here, we have a variable, val3, that had a value (1), and now we want to unset it so it can't be used, and set it equal to null.1007

We pass it to this unset construct, and simply...the way you do this: you write unset, and then,1015

in between parentheses, you put the name of the variable.1019

So, after having done all three of these declarations and this unset, we can compare each of these variables to null,1022

to see if they actually are null, as we expect them to be.1032

So, down here, there are three separate operations; we are starting to use the comparison operator already.1036

We are saying, "Is val1 equal to the null literal?" meaning "is it equal to null?"1042

And because this is a comparison operation, it returns true or false.1048

So, if it is equal to null, it's going to return true; and if it's not equal to null, it's going to return false.1051

Because we just explained that these three ways are how you set a variable to null, we expect all of these to be equal to true.1055

And if we go and load the page, which is going to output these three boolean variables that we have (isNull1, isNull2, and isNull3),1064

we can see that, when we explicitly set something to null, that one equals null.1078

We said, "Is null equal to the result of comparing val1 to null?"--we see that isNull1 is true, because this value is equal to null.1083

Now, we have the val2 variable, which had been declared but not initialized.1094

We tested if that is equal to null, and the output of that is that it is true--it is equal to null.1098

And then, the third way to set something to null is to use the unset function for a variable that has previously been initialized.1105

And so, we had val3 that had the value of 1; now, we unset it, and when we compare val3 to null, we get that the result is true.1111

Really quickly, I just want to mention the coding conventions that we use in this course.1123

For both the boolean literals and the null literal, we are always going to use the all-uppercase versions of these.1128

These are the two boolean ones, and that is what we are going to use for null.1133

That is probably the most common convention out there.1138

Additionally, we had mentioned that the not equal operator has another version, which is this less than, greater than operator.1141

In this course, we are always going to use the exclamation point, equal version of that operator; that is the more common one used.1148

For homework, I just want to have you go over the comparisonOperations.php page 1159

and make sure you understand how all the comparisons work--the greater than or equal to, the greater than,1165

equal, not equal, and so forth, as well as the identical ones, to make sure you understand the subtle difference between 1169

the equal operator and the identical operator, and also between the not equal operator and the not identical operator.1176

That is in comparisonOperators.php, which is one of the scripts we just learned.1183

And also, I'll have you review null.php, the script that we just looked at, and the slide on the null data type,1188

to make sure you understand the concept of null, which is the absence of a value,1194

and to see the three different ways in which a variable can end up with the value null, or with the data type null.1199

That ends today's lesson; thank you for watching look forward to seeing you at the next lesson.1206