PHP Basic Booleans

True or False?

The PHP boolean value is a scalar data type that represents the values true and false. That’s really it. End of article. That would be a short article so lets take a look at a few examples before we abruptly end it. Create a new file and name it booleans_intro.php. We’ll be testing out the code below with our booleans_intro.php file.
One of my favorite vehicles, and this is important for understanding booleans properly, is the MKIV 2JZGTE Toyota Supra. We’ll create a variable to check whether that Supra currently exists in my garage or not.

 

$in_garage_mkiv_supra = false;
var_dump( $in_garage_mkiv_supra );

 

Unfortunately, I had to set that variable to false. If you var_dump() the value, it will show you that the variable is storing a boolean type and the value is false. Let’s continue on with a few more examples. The next car that we’ll check is to see if the Subaru WRX STi is inside the garage.

 

$in_garage_sti = true; // hooray...one down
var_dump( $in_garage_sti );

I’m going to briefly introduce you to the if statement even though we’ll cover it extensively in Part 24 of this article series. Just say it in English:

if something then something

So, if something is true, then do something. You do if statements constantly throughout the day. For example, I’ll wash the car only if it’s sunny outside. That can be translated to

if (sunny == true) then: wash car.

Now that we have a very basic understanding of what the if statement does, let’s do some testing on our previous code.

 

$in_garage_sti = true;if ( $in_garage_sti == true ) {
  echo "Hooray, I have AWD";
}

 

Since the value that’s stored inside the variable is already either true or false, we don’t have to compare it to true or false. We can just drop the comparison operator and the true keyword and just leave the variable inside the if statement.

 

$in_garage_mkiv_supra = false;if ( $in_garage_mkiv_supra ) {
  echo "I'll be sleeping in the garage tonight.";
}

 

The if statement evaluates to false since the $in_garage_mkiv_supra variable is a boolean that holds the value false, and the code inside the if statement is never executed. Unfortunately, it’s the comfy bed for me tonight.

We can also make boolean variables true if they’re false and vice versa with the negation operator (!). If you variable is set to false, and you use the negation operator right in front of it, it will convert it to false. It just flips the boolean value.

 

$in_garage_r32_skyline = false;if ( !$in_garage_r32_skyline ) {
  echo "WoooHooo I got a Skyline"; // I really don't :(
}

 

The above statement looks like it would not evaluate the body of the if statement, but it actually does. Why? If we said it out loud, we would say:

  1. if not $in_garage_r32_skyline
  2. if not false
  3. if true

Since the if statement evaluates the expression to true, it evaluates the body of the if statement and now tears are flowing down my face because I just realized that I don’t have a skyline, even though the statement says that I do.

Let’s look at something that might not be as obvious. We’re going to be looking at comparison operators soon, but I’m assuming that you took math and have seen the greater than and less than operators: >, <.

I can ask you the question “is it true that 5 is greater than 2?” And you would reply, “yes, that is a true statement.” Since that evaluates to either true or false, we can set a variable to be true or false based on it evaluating an expression.

I would like to see if I’m rich. I currently have $10 in my bank account but need $100,000 in my bank account to be considered rich. Am I rich? How would we model that.

 

$current_money = 10;
$rich = $current_money > 100000;
var_dump( $rich );

 

If you save the code above into your file and open it up in your browser, you’ll see that var_dump will output false. Why? PHP first looks at the expression on the right of the assignment operator (which we haven’t looked at in detail yet). It substitutes 10 for $current_money and then evaluates the expression 10 > 100000. That is not a true statement, so it returns false. The value false is then assigned to the variable $rich.

 

 

Now I think I can say that the article is over.

 

PHP Tutorial Series

Continue your learning with these articles

PHP Variables

CAN YOU EVEN STORE DATA?

PHP – P3: VARIABLES INTRO

Variables are just storage containers. You can store any data type that you would like inside of them.

PHP Basic Booleans

TRUE OR FALSE?

PHP – P4: BASIC BOOLEANS

The boolean value is a scalar data type that represents the values true and false. That’s really it. End of article. That would be a short article so lets take a look at a few examples before we abruptly end it.

PHP Truthy Bools

What Do You Mean Truthy?

PHP – P5: TRUTHY BOOLS

Booleans represent true or false. PHP has truthy booleans, which are values that are not true or false, but when evaluated inside an expression, they can act like they are.

Leave a Reply