PHP Boolean Data Type - Full PHP 8 Tutorial

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
[Music] a boolean is a simple representation of a truth value it can either be true or false so if we have a variable called is complete we can assign either true or false to this and the true and false are the predefined constants that come with php and they are also case insensitive so you can set this to true or lower case or you can use all uppercase or you can mix and match uppercase and lowercase letters though i highly recommend to just stick with one and stay consistent i personally prefer all lowercase so that's what i'll be using booleans are mostly used within control structures like loops or ethos conditionals don't worry about the control structures for now we'll talk about those in a separate video but it would look something like this if is complete do something otherwise do something else what this means is that this section right here will only evaluate if the expression evaluates to true otherwise this section will run now what do i mean by an expression evaluating to true this for example is a variable and the variable is set to true so this evaluates to true and therefore this section will run right here if we were to set this to false then this section would execute now there are other data types that could be converted to booleans on the fly by php and they could be evaluated to either true or false for example integers 0 and negative 0 will always be considered as false same applies to floats 0.0 and negative 0.0 will also evaluate to false then we have strings if you have an empty string this will also evaluate to false or if you have a 0 that is a string within quotes like this it will also evaluate to false an empty array for example like this will also evaluate to false and the special type null will also evaluate to false anything else pretty much will evaluate to true even the negative numbers that are not zero so let's test this out if something evaluates to true let's print out success and otherwise let's print out fail right now is completely set to false so if we refresh the page we say fail let's set that to true now this will say success now let's set this to an integer number 5 and this will say success still because 5 evaluates to true we can set this to negative 0 and this will evaluate to false and it will print fail we can set this to an empty array and this will evaluate to fail as well we can set this to a non-empty array and this will be success now how can you actually print the boolean now one way to print it is by using conditional like this where you don't actually print a boolean but instead when something is true you print something else like this for example another way that you've seen before is just simply echoing out the boolean so if we echo out is complete and set this to true it will echo out as one let me comment this out for now and we see one if we set this to false we see nothing because it's just an empty string now you saw this in previous video but why does it actually print an empty string for false and why does it print one for true the reason for that is because when you're printing something on php it will try to cast it into a string so it's essentially doing same thing as this right here it's just you don't need to specify string in here and when boolean is cast into a string false becomes an empty string and true becomes one and we can check that by using var dom so let's do var dump is complete and we see that it's boolean type with the false and now let's convert it to string by using the cast and we see that it's a string type with zero characters in it so that's what happens when you do echo is complete and if we do this to true this becomes string with the value of one so as you know var dump is one way to check the type whether it's string or integer or boolean another way to check type for a boolean is by using a function called easbu and that will return true or false depending on whether the variable is true or false so in this case if we var dump the value of isbul it's going to return false because we're casting it into string if we change this back to true now it will return true because it's a boolean value if we change this to an empty array this is false and one more thing i want to mention before wrapping up this video is that if you have a string that's equal to false you might think that this would evaluate to false but it would actually evaluate to true because as mentioned right in here anything that's not an empty string or a zero it will evaluate to true so even though this says false here this is not an empty string and it's not a zero and therefore this will evaluate to true in here so if we refresh the page we see success this is it for this video in the next video we're going to talk about integers so thank you for watching please give this video a thumbs up share and subscribe and i'll see you on the next one
Info
Channel: Program With Gio
Views: 17,966
Rating: undefined out of 5
Keywords: php, php8, learn php, php tutorial, php course, learn php the right way, beginner friendly php course, full php course, php in 2021, advanced php course, beginner to expert php, boolean data type, php booleans
Id: 1kO_g_ucYCQ
Channel Id: undefined
Length: 4min 44sec (284 seconds)
Published: Thu Dec 03 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.