PHP Tutorial (& MySQL) #10 - Booleans & Comparisons

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
alright then gang so I think now I'd like to talk a little bit about boolean x' which is a new data type in PHP we've not talked about yet and comparisons and actually in the last video we've already looked at kind of buildings and comparisons without even knowing it when we did loops because what we did is have this array of products right here and we had a while loop and inside this while loop we had this condition right here this is a condition that we're evaluating and that's either going to be true or false right if I is less than the count of products then this right here is essentially true this statement this comparison is true and we're going to execute the code if I was like 20 and it wasn't less than the count of products then this condition right here is going to evaluate to false okay and then the code isn't going to execute inside the loop so true and false they are two special values in PHP and they are their own type they are boolean's ok and we use billions to execute conditional code like this we perform a comparison that comparison is going to return some kind of boolean and that billion if it's true will mean this runs if it's false it won't run okay so that's kind of the crooks of boolean's and comparisons so let's delete this bump for now and let's have a look at these different billions so what I'm going to do is echo both a true boolean and also a false boolean to the screen so let me say echo true first of all and spell echo correctly and save this preview in the browser and we see one we don't see the word true we see one now before I explain this let me now echo instead false let's see what we get and this time we shouldn't get anything it should be an empty string so why is this why do we not see actually true and false as words on the screen well whenever we echo a boolean to the browser we don't see the texture Awful's when we echo to the browser values have to be converted to strings and everything that's output to the browser is ultimately a string of text or numbers or characters now PHP when it sees a boolean and we want to output it to the screen it converts it into either a string of 1 if it's true or a string of empty if it's false so in this case it would be a string of 1 like this and in this case it would be an empty string like this and that's what it echoes to the browser because every time we echo something to the browser we have to output a string right every time a browser shows you something it's essentially a string of text a browser doesn't know here what true or false is okay so every time we output to the browser we have to convert things to a string now the reason it uses 1 and an empty string for true and false is because this in loose comparison and I'll talk about loose comparison later on is a truth value it would evaluate to true ok and this right here would evaluate to false so they're easy to convert back so that's why it uses 1 and 0 and that's why we don't see true or false in the browser so anyway that's the basics of what these billions are true or false so let's do a few comparisons now and see whether they return true or false so first of all let's do some comparisons with some numbers so the first one I'm going to do is echo and we'll say 5 is less than 10 now me and you know that this is true so this should evaluate as true and because it evaluates is true and we're echoing that to the screen it should be a 1 in the browser right so let's save that and preview and we get one cool so yeah this is true ok next one let's this time echo and we'll say 5 is greater than 10 and in fact what I'm going to do is just a couple here so echo 5 is equal to 10 so I'll explain this in a second first of all 5 is greater than 10 well that's false so we shouldn't get anything on the screen for that so let's just comment this one out and see if we do and no we don't get anything perfect that's good and now let's comment this one out we're saying 5 is equal to 10 now notice I've used two equal signs here I'm not using one because when you use one equal sign that is an assignment operator for example would say something like name is equal to Shan right and we're trying to set the value of this variable equal to this and right here it's the same we're trying to set the value of five equal to ten that doesn't make sense when we're using comparisons if we want to check something is equal to one another we can use double equal signs that is what is known as a loose comparison we can use triple equals and we'll talk about that later but for now just double equals is fine so we want to know if five is equal to ten obviously we know it's not so we should see nothing an empty string in the browser which we do cool okay so let's do a couple more now I'm gonna echo 10 is equal to 10 and scoot this in save that see what we get and we get one because they are equal cool okay let's do some more I'm gonna say echo five is not equal to 10 now this right here this is a negation operator so if you want to see if things are equal to one another we use these double equals or treble equals if you want to see if something is not equal to each other then we use an exclamation mark than an equal sign okay so 5 is not equal to 10 this is obviously true so then we should get a 1 on the screen refresh and we get that one cool all right so let's do just a couple more I'm gonna say echo 5 is less than or equal to 5 so I don't think we've seen this yet either if we do a less than sign then it just means 5 is less than 10 for example at the top if we do less than followed by an equals it means 5 is less than or equal to 5 so this is true right because it's equal to 5 it doesn't have to be less than less than or equal so we should see a 1 on the screen which we do ok let's do one more we'll say echo 5 is greater to or equal to 5 again and again this should be true because although it's not greater than it's still equal to 5 so if we save this and preview then it works okay we get at one perfect okay then so that's some comparisons with numbers and we're going to be using all of this kind of stuff as we go forward now we can also do comparisons with strings so let's do a little comment sing strings and I'm gonna say first of all echo oops Sean is less than Yoshi now you might be thinking well how can one string be less than and not other not numbers how can you compare them like this and the weight works is if this letter is before this letter in the alphabet then this is less than this so this should be true because s comes before Y okay so if we save that we should see a 1 on the screen and we get that perfect okay next example let's say echo Shan is greater than Yoshi now this is the opposite so it should be false right so we don't get anything perfect alright next let's do a slightly different example we'll say echo Shan is greater than Shan with a capital S now then what's going on here well first of all let's try this out save it what do you think will happen refresh and we get a one so we're saying this is greater than this so when we use an uppercase letter an uppercase letter is seen as less than a lowercase letter so this is more than this okay all right then so next example we'll say echo Mario is double equal to Mario so are these things equal let's save it and find out refresh and we see a 1 so they are okay let's do one more example I'm gonna say echo Mario is equal to Mario with a capital M and do you think this will work will this be true or false well it should be false because m and M are not equal remember this thing here is greater than this thing here okay so that's how it can compare strings as well now I did say I was going to talk a little bit more about loose comparison which is this double equal stuff and strict equal comparison which is triple equals so let's have a little look down here when we use a double equals we don't take into consideration any kind of type okay and by type I mean data type so for example I could say echo v is double equals to the string v now technically they're not equal to each other but when we use loose comparison which is only two equal signs then it doesn't take into consideration the type of data that we're using it's going to look at this and say well okay I can see what you mean we've got a five here is a string five here as a number yep they're kind of equal they're loosely equal so I'm going to return true for you so if I save this we should see a one in the browser okay now then if I comment this out and if I do the same thing but this time use triple equals like this this is a strict comparison and that does take into account the type of data so it's going to look at this and say even though both of these are fives they're not the same data type so I'm not going to consider this to be true so if I save this and refresh we should get nothing perfect okay so that's the difference between a loose and strict equal comparison now then obviously if I do echo five is equal to five that's going to be true and also echo five is triple equals to five that's going to be true because in both cases five is equal to five and with triple equals that both the same data type so yep is true okay we don't need to test that we know it's true so that there is loose vs. strict equal comparison now a lot of the time it's wiser to use this kind of exact match this strict comparison because then there's less room for error okay now I did say at the start that when we output boolean's to the browser it converts them into these strings which are 1 and an empty string now I want to show you using loose comparison that this is true thing it's equal to true in a loose comparison but they don't take into account types remember in loose comparison so let me just echo out true is equal double equals loose comparison to the string 1 they are one and the same in loose comparison so save that and preview and we get a 1 and I can do the same thing with false so I'm gonna say echo false is equal to an empty string and that should be true as well because those two are the same thing in loose comparison so let's save this and preview and we can see we get a 1 there as well alright so there we've got my friends and know that was kind of tedious and we did a lot of repetition there but I wanted to show you all of these different comparisons that we can do less than greater than loose comparison strict comparison equal to etc and talk about billions because they are a big part of PHP and we're going to be using them a lot especially starting in the next lesson where we're going to look at conditional statements
Info
Channel: The Net Ninja
Views: 45,802
Rating: undefined out of 5
Keywords: php, tutorial, php tutorial, php tutorial for beginners, mysql, mysql tutorial, mysql tutorial for beginners, sql, sql tutorial, php for beginners, learn php, php mysql, php and mysql, boolean, booleans, php boolean, php booleans, boolean tutorial, true, false, truve vs false, true false, php tru, php false, truthy, falsey, boolean values
Id: hxYQA-nuIXY
Channel Id: undefined
Length: 12min 7sec (727 seconds)
Published: Thu Feb 07 2019
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.