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. 0:00 This is actually the simplest type of variable because a Boolean can be 0:04 only one of two values, either true or false. 0:09 There is no middle ground or gray area. 0:13 There is no variation on truth. 0:15 It is or it isn't. 0:17 So let's see how we define a variable that will be considered a Boolean variable. 0:19 We'll also explore more of the basic features that PHP offers. 0:25 In the strings.php file, let's add a few more lines at the end. 0:30 We'll create a new variable and we call this isReady. 0:36 We're going to set this equal to the keyword 0:41 true which will make this a true Boolean. 0:44 PHP itself is not case sensitive when it comes to Boolean values. 0:50 So you can write this in any case you want. 0:54 There are conflicting standards on whether these should be all uppercase or 0:55 all lowercase choose a standard and be consistent. 1:00 I'm using the PHP standards recommendation 1:04 put out by the PHP Framework Interop Group, or the PHP-FIG for short. 1:07 The PHP-FIG is a group of established PHP projects 1:11 whose goal is to provide easier ways for PHP programmers to work together. 1:15 The PSR 2 coding style guide states that true and false must be in lowercase. 1:20 To learn more about the PHP FIG and PSR 2, check the teacher's notes. 1:25 So now if we want to redefine this as false, 1:30 we could simply redefine this variable. 1:34 And this time, set it equal to false. 1:39 This brings us to another important part of PHP. 1:43 The order of operation. 1:44 PHP processes statements in a top down step by step manner. 1:47 So first we set this Boolean equal to true. 1:52 And then we redefine it on line 14 to be equal to false. 1:54 But how do we know we're actually assigning the value of this variable? 1:59 Let's use our var_dump function again. 2:03 I told you it was super useful. 2:05 We'll pass it isReady, then we'll duplicate this line below the false. 2:10 Now when we run our code we can see what's happening. 2:19 First, our Boolean is ready is set to true. 2:23 And then our Boolean is ready is set to false. 2:27 Booleans are often used to control the flow of the program 2:31 by testing program conditions using what is called an if statement. 2:35 If a user is logged in then show them their account details. 2:40 We've looked at how to modify a Boolean value by 2:45 simply using the keyword true or false. 2:48 We can also modify a Boolean value based on a comparison of other values. 2:50 In the next video will take a brief look at how these comparison operators work. 2:56
You need to sign up for Treehouse in order to download course files.Sign up