Bummer! This is just a preview. You need to be signed in with a Basic account to view the entire video.
Start a free Basic trial
to watch this video
Booleans are the simplest type of variable because a Boolean can only be one of two values: either true or false. There is no middle ground or gray area. There is no variation on truth, it either is or it isn't. We'll examine the way PHP views Boolean values and explore more of the basic features that PHP offers.
Documentation
Booleans are the simplest type. A boolean expresses a truth value. It can be either TRUE or FALSE.
Examples
var_dump((bool) ""); // bool(false)
var_dump((bool) 0); // bool(false)
var_dump((bool) 1); // bool(true)
var_dump((bool) -2); // bool(true)
var_dump((bool) "foo"); // bool(true)
var_dump((bool) 2.3e5); // bool(true)
var_dump((bool) array(12)); // bool(true)
var_dump((bool) array()); // bool(false)
var_dump((bool) "false"); // bool(true)
PHP Framework Interop Group (PHP-FIG) is a group of established PHP projects whose goal is to talk about commonalities between our projects and find ways we can work better together.
PHP Standards Recommendations (PSR-2) intent is to reduce cognitive friction when scanning code from different authors. It does so by enumerating a shared set of rules and expectations about how to format PHP code.
2.5. Keywords and True/False/Null
PHP keywords MUST be in lower case.
The PHP constants true, false, and null MUST be in lower case.
-
0:00
The next scalar type we'll be covering is Booleans.
-
0:04
This is actually the simplest type of variable because a Boolean can be
-
0:09
only one of two values, either true or false.
-
0:13
There is no middle ground or gray area.
-
0:15
There is no variation on truth.
-
0:17
It is or it isn't.
-
0:19
So let's see how we define a variable that will be considered a Boolean variable.
-
0:25
We'll also explore more of the basic features that PHP offers.
-
0:30
In the strings.php file, let's add a few more lines at the end.
-
0:36
We'll create a new variable and we call this isReady.
-
0:41
We're going to set this equal to the keyword
-
0:44
true which will make this a true Boolean.
-
0:50
PHP itself is not case sensitive when it comes to Boolean values.
-
0:54
So you can write this in any case you want.
-
0:55
There are conflicting standards on whether these should be all uppercase or
-
1:00
all lowercase choose a standard and be consistent.
-
1:04
I'm using the PHP standards recommendation
-
1:07
put out by the PHP Framework Interop Group, or the PHP-FIG for short.
-
1:11
The PHP-FIG is a group of established PHP projects
-
1:15
whose goal is to provide easier ways for PHP programmers to work together.
-
1:20
The PSR 2 coding style guide states that true and false must be in lowercase.
-
1:25
To learn more about the PHP FIG and PSR 2, check the teacher's notes.
-
1:30
So now if we want to redefine this as false,
-
1:34
we could simply redefine this variable.
-
1:39
And this time, set it equal to false.
-
1:43
This brings us to another important part of PHP.
-
1:44
The order of operation.
-
1:47
PHP processes statements in a top down step by step manner.
-
1:52
So first we set this Boolean equal to true.
-
1:54
And then we redefine it on line 14 to be equal to false.
-
1:59
But how do we know we're actually assigning the value of this variable?
-
2:03
Let's use our var_dump function again.
-
2:05
I told you it was super useful.
-
2:10
We'll pass it isReady, then we'll duplicate this line below the false.
-
2:19
Now when we run our code we can see what's happening.
-
2:23
First, our Boolean is ready is set to true.
-
2:27
And then our Boolean is ready is set to false.
-
2:31
Booleans are often used to control the flow of the program
-
2:35
by testing program conditions using what is called an if statement.
-
2:40
If a user is logged in then show them their account details.
-
2:45
We've looked at how to modify a Boolean value by
-
2:48
simply using the keyword true or false.
-
2:50
We can also modify a Boolean value based on a comparison of other values.
-
2:56
In the next video will take a brief look at how these comparison operators work.
You need to sign up for Treehouse in order to download course files.
Sign up