PHP Tutorial (& MySQL) #11 - Conditional Statements

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
so then a big part of any programming language is this idea of conditional statements and by that I mean check a certain condition if that condition passes or if it's true then do one thing if it doesn't pass then maybe do another instead a good example of this would be if a user is logged in we might want to check that at some point if they're logged in and that test passes we'd do something if they're not logged in we do something else okay that could be if they're logged in we show them one navigation if they're not logged in we show them a different navigation so these kind of things are conditional statements another example might be that we have a lot of products we want to cycle through them and for each product we want to check the price of that product if it's below a certain price we want to output it to the screen if it's above a certain price we don't want to output it to a certain screen these are all kind of conditional statements they're like Forks in the code where the code could go one way or the other okay so that's what we're going to talk about in this lesson and we're going to use some of the stuff we learned from the last lesson about boolean x' and comparisons to do this because at the end of the day we're gonna make comparisons or we're going to make certain conditions and they have to be evaluated so then the first thing I'd like to do is create just a simple variable and I'll call this price and I'm going to set that equal to an integer 20 now then I want to do a quick check on this price variable I want to check if it's below a certain amount so I could say if and then in brackets price is less than 30 then what we're going to do is do something we're going to fire at some code right so this is an if statement it's a conditional statement we use the if keyword then we have some kind of condition we want to evaluate right here we're saying is the price less than 30 if this condition is true right here then what's going to happen is this block of code inside here is going to execute all right so we could say echo and then we'll say the condition is met okay so then if I save this now and go over to the browser and refresh we can see the condition Mette okay then so what if we want to do something else if that condition is not met for example if we change this to ten and we run this then we see that is not output to the browser what if we want to do something else if that's not the case well we can add on an else clause so we can say if this condition is true if this evaluates to true then we're going to echo this to the browser if it doesn't then we'll run the else clause so inside this will say echo and then condition not met okay so save that this time if we refresh we can see the condition is not met so that works now simply enough right we're just checking a simple condition right here and we're firing either this block of code or this block of code dependent on the result of this if it's true or false so then we can also add in additional clauses additional statements that we can evaluate if you like so the way we do this is by saying else if and then another set parentheses another code block like so so what we're going to do here is check this first of all if this condition is true then we'll just echo this and we won't carry on with the rest if this condition is false we'll move on to the else if and we'll check this condition right here so now I'll say if price is less than 20 and then we'll echo something in here if this is the case so we'll say else if condition met just so we know and then if this is true this will fire this block of code if it's not true it moves on to the next one the else condition the catch-all if you like and that will fire instead okay so we do these in order this then this then finally this if none of these are true so this isn't true and this isn't true so again this should fire because price is 20 is not less than 20 is 20 so let's save this and preview this in a browser and we can see condition is not met awesome okay then so if we change this to 30 see right here then we should get this firing because the price is less than 30 so save this and refresh and we can see now the elsif condition is met cool so it might seem pointless this because we're writing this out as we know the price and we know that it's less than 30 and we know that it's more than 10 so why bother doing all of this and the reason is we might not always know the value of something when we check in this we might get it from a database a list of products and in those list of products we could have different prices and we want to go through those and only output them if they're under or over a certain price so we might not know their price okay so that's why we do this kind of thing so then that's basically if statements now what I've done is saved some of this code from a previous tutorial and this is just a product variable equal to an array of arrays it's a multi-dimensional array and the arrays inside these are all associative arrays and in each array we have a name key and we have a price key so a product name and a product price so what I'd like to do now is use first of all a for each loop to cycle through each one of these we've already covered those loops and then inside the loop for each iteration round for each product what we're going to do is a quick check if the product price is maybe less than something or with something then we're going to output it okay so then let's go down here and do this for each loop first of all we'll say for each and then in brackets products as product okay so I'm using the singular version of the products array and I'm calling each item of products each time around then in here for each one we want to do a little ear check so then I'm going to say if the product that we're currently cycling over and we want the price property from that product and we're going to check if that is less than 15 and if it's less than 15 then what we're gonna do is output the product name we're going to echo it so let me just scoot this in now I'm gonna say echo the product itself and we want the price okay and we'll also concatenate a br tag so that each time around would go on to a new line okay then so if we save that and preview it let's refresh over here we see 10 5 & 2 oops we want the name not the price the name of each product so we get the green Scheldt the gold coin and the banana skin so these are the products where the price is less than 15 because for each product we're checking this and it's true if the price is less than 15 then without putting the name otherwise we're not outputting the name right so it would be this one the green shell it would be this one the gold coin and this the banana skins that is what we saw in the browser awesome okay then now we can check if we want multiple conditions inside if statements so we could say okay well we want the price to be less than 15 but we also want the price to be greater than 2 right this isn't greater than 2 so we don't want to output this now the way we check for multiple conditions is by either nesting our if statements so we could do another if statement in here to check if the product price is also over 2 but that's a bit of a waste of time instead we use a special double and operator and that means we want to check another condition as well and that condition is going to be that the product price is over 2 okay so now we're saying okay well only execute this code if the product price is less than 15 and the product price is greater than 2 so now we shouldn't see this one banana skin so let's save it and preview it and we can see the banana skin has vanished cool okay then so let me just copy this and then I'm going to comment it out because underneath I'm going to do a different example so this time I'm still going to check two things the first thing I want to check is if the price is over 20 now the second thing I want to check is if the price is less than whoops less than 10 now obviously something can't be over 20 and less than 10 that's just impossible so I'm gonna change this into an all and that is a double pipe now the pipe symbol can be found on most keyboards left every space I have to press shift and that key twice and this means or it's a logical or so now we're checking these two conditions and only one of them has to be true so we're saying if the price is over 20 or if the price is less than 10 then we can output the product main does that make sense so if it's like if it's greater than 20 it's gonna be this that's the only one greater than 20 and less than 10 is this and this so we should see these three products gold coin lightning bolt and banana-skin output see the browser so let's save that and view this and voila we get those cool right okay then so that's how we use if statements to check certain conditions if they're true we execute code if they're not true we can execute something else maybe with an else--if or an else statement okay so that's that now what about outputting if statements inside the template itself because sometimes we want to output different content if something is true and different content if something is false well let's do a little example here what I'm going to do first of all is a div tag and inside this div I'll do it you out now I'm gonna do a for each loop here to cycle through the products so I'm gonna do my PHP tags and then say for each and inside I'm going to say products as product and then open up my loop now remember we need a separate PHP tag down here to close the loop later on because in between we're going to output some HTML template code okay so when were inside this loop I'm gonna do an if Jack now now I'm gonna do PHP to open up my tags again and by the way I could have just left this off and started the if statement but I like to do each line of PHP code in HTML templates as its own PHP tag to me it just looks cleaner and I know where everything is so I'm gonna say if then inside the product and it's going to be the price that we're looking for if that price of the product is greater than 15 then what we're going to do is output the price so again open your curly braces and then we'll close them down below over here so PHP tags and close them there so this is where we can output the Li tag now so we're cycling through all the products if the price is greater than 15 we're going to output an li tag for that product so we'll say PHP inside ear to echo the product and we want the name this time okay alright then so the letters now save this cross our fingers and hope it works refresh and now we can see we get the shiny star and the lightning bolt and that is anything with a price over 15 over here so we can see this one which is the lightning bolt and the shiny star which is 20 sweet so there we go my friends that is how we can use if statements to check things and even output different content in the Dom if a certain check passes
Info
Channel: The Net Ninja
Views: 43,635
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, if statements, if, elseif, else if, else, php if, if statement, php elseif, elif, php if statement, php else, php else if
Id: E1ms4qpfy78
Channel Id: undefined
Length: 12min 10sec (730 seconds)
Published: Fri Feb 08 2019
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.