Start learning with our library of video tutorials taught by experts. Get started
Viewed by members. in countries. members currently watching.
PHP is a popular, reliable programming language at the foundation of many smart, data-driven websites. This comprehensive course from Kevin Skoglund helps developers learn the basics of PHP (including variables, logical expressions, loops, and functions), understand how to connect PHP to a MySQL database, and gain experience developing a complete web application with site navigation, form validation, and a password-protected admin area. Kevin also covers the basic CRUD routines for updating a database, debugging techniques, and usable user interfaces. Along the way, he provides practical advice, offers examples of best practices, and demonstrates refactoring techniques to improve existing code.
In this movie, we're going to look at Booleans. A Boolean is a programming type that can either be true or false. That's it, one of those two values. And true is not the string true or even the number one. It's just simply the value true. Booleans are very useful in programming because we can use them when performing tests. For example, in the last movie, we used the in array function to test whether an integer was inside of an array. The result was that the test was a Boolean, true if the integer was present, false if it was not. Let's start by creating ourselves a new workspace for this.
I'll open up basic .html, I'll do Save As, and we're going to call this Booleans. Notice how that's spelled Booleans, and I'll also call it Booleans here. And let's create our PHP tags. Now, a Boolean is simply the value true or the value false. Notice that there are no quotes around that. It's not a string, so we could have for example, result1 equals true. And then down here, we'll have result2 equals false.
Now what we're doing is assigning a Boolean to a variable. We can assign it just like we do anything else. And we can take a look at what these values look like when we echo them back. I'll just save that, result1 and result2 being echoed. So, we'll just go to our browser. And instead of array functions, let's now load up booleans.php. Notice that result one comes back and says one, result2 doesn't have anything. We've seen this before. One is what happens when PHP converts a Boolean true into a string, it turns it into one. When it's converting a Boolean false into a string it has nothing. It outputs absolutely nothing, not a zero.
But actually no string at all. Now we can assure ourselves that result two is in fact a Boolean by calling another function here called is_bool. That will tell us whether or not something is a Boolean. It's just like we had is numeric earlier, is float. Well, we have is bool to find out if something is a Boolean. And what is it going to return to us? A Boolean, but the difference is that if result2 is a Boolean, it's going to return true which we'll be able to see because that'll come back as being a 1. And notice that I have true and false all lower case here. In PHP, you'll often see it written all upper case TRUE and FALSE, or you'll see it lower case.
And you can waste a lot of time arguing with people about the right way of doing it in PHP. Everyone has their belief about whether they should be upper case or lower case. But in truth, in PHP, they're case insensitive and you can use which ever one you prefer. They're absolutely equal. I prefer to use the lower case version. Now, when Booleans are going to be most useful to us is when we're working with conditionals which we haven't seen yet. But the basic idea is in our code we want to say if one thing is true. Then, do this bit of code if something else is true then do this other bit of code.
Maybe a little sneak peak of conditionals even though we're not quite ready to dive into them yet I'll give you a preview. Let's say that we have number equal to 3.14, and we're going to write an if statement. Don't worry too much about the format of the if statement. We'll be covering this later. But, I'm going to check to see whether number is a float, right? We saw that is_float earlier, and it returns a boolean as a result. So if it returns true, then echo it is a float.
There we go. Let's just try that out. And there it is. You see that it is a float. And if we change it so it's not a float, make it a three. We reload the page, you'll see that it doesn't execute that code. So, that's where Booleans becomes really useful. We're working with conditionals and we're going to do a lot of that when we're programming. So, we'll see more of that when we come to the conditional section. For now, just make sure that you understand the basics of what a Boolean is and that a Boolean is not a string or an integer It is its own type. And it's always either true or false.
Find answers to the most frequently asked questions about PHP with MySQL Essential Training.
Here are the FAQs that matched your search "":
Sorry, there are no matches for your search ""—to search again, type in another word or phrase and click search.
Access exercise files from a button right under the course name.
Search within course videos and transcripts, and jump right to the results.
Remove icons showing you already watched videos if you want to start over.
Make the video wide, narrow, full-screen, or pop the player out of the page into its own window.
Click on text in the transcript to jump to that spot in the video. As the video plays, the relevant spot in the transcript will be highlighted.