Bummer! This is just a preview. You need to be signed in with a Basic account to view the entire video.
Booleans3:02 with Alena Holligan
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.
Booleans are the simplest type. A boolean expresses a truth value. It can be either TRUE or FALSE.
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.
The next scalar type we'll be covering is Booleans.
This is actually the simplest type of variable because a Boolean can be
only one of two values, either true or false.
There is no middle ground or gray area.
There is no variation on truth.
It is or it isn't.
So let's see how we define a variable that will be considered a Boolean variable.
We'll also explore more of the basic features that PHP offers.
In the strings.php file, let's add a few more lines at the end.
We'll create a new variable and we call this isReady.
We're going to set this equal to the keyword
true which will make this a true Boolean.
PHP itself is not case sensitive when it comes to Boolean values.
So you can write this in any case you want.
There are conflicting standards on whether these should be all uppercase or
all lowercase choose a standard and be consistent.
I'm using the PHP standards recommendation
put out by the PHP Framework Interop Group, or the PHP-FIG for short.
The PHP-FIG is a group of established PHP projects
whose goal is to provide easier ways for PHP programmers to work together.
The PSR 2 coding style guide states that true and false must be in lowercase.
To learn more about the PHP FIG and PSR 2, check the teacher's notes.
So now if we want to redefine this as false,
we could simply redefine this variable.
And this time, set it equal to false.
This brings us to another important part of PHP.
The order of operation.
PHP processes statements in a top down step by step manner.
So first we set this Boolean equal to true.
And then we redefine it on line 14 to be equal to false.
But how do we know we're actually assigning the value of this variable?
Let's use our var_dump function again.
I told you it was super useful.
We'll pass it isReady, then we'll duplicate this line below the false.
Now when we run our code we can see what's happening.
First, our Boolean is ready is set to true.
And then our Boolean is ready is set to false.
Booleans are often used to control the flow of the program
by testing program conditions using what is called an if statement.
If a user is logged in then show them their account details.
We've looked at how to modify a Boolean value by
simply using the keyword true or false.
We can also modify a Boolean value based on a comparison of other values.
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