PHP Tutorial (& MySQL) #11 - Conditional Statements

Net Ninja · Beginner ·🛠️ AI Tools & Apps ·7y ago

Key Takeaways

Uses PHP to implement conditional statements with if and else if

Full Transcript

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

Original Description

Hey gang, in this PHP tutorial I'll talk about conditional statements (if, else if etc). We conditionals to branch our code depending on whether a certain condition is true or false. ---------------------------------------- 🐱‍💻 🐱‍💻 Course Links: + Course files - https://github.com/iamshaunjp/php-mysql-tutorial + VS Code editor - https://code.visualstudio.com/ + Materialize Playlist - https://www.youtube.com/watch?v=gCZ3y6mQpW0&list=PL4cUxeGkcC9gGrbtvASEZSlFEYBnPkmff 🤑🤑 Donate + https://www.paypal.me/thenetninja 🎓🎓 Find me on Udemy + https://www.udemy.com/user/47fd83f6-5e4a-4e87-a0f0-519ac51f91b6/
Watch on YouTube ↗ (saves to browser)
Sign in to unlock AI tutor explanation · ⚡30

Playlist

Uploads from Net Ninja · Net Ninja · 0 of 60

← Previous Next →
1 Regular Expressions (RegEx) Tutorial #14 - Matching a Username
Regular Expressions (RegEx) Tutorial #14 - Matching a Username
Net Ninja
2 Regular Expressions (RegEx) Tutorial #15 - Email RegEx Pattern
Regular Expressions (RegEx) Tutorial #15 - Email RegEx Pattern
Net Ninja
3 Regular Expressions (RegEx) Tutorial #16 - Finishing Touches
Regular Expressions (RegEx) Tutorial #16 - Finishing Touches
Net Ninja
4 GraphQL Tutorial #1 - Introduction to GraphQL
GraphQL Tutorial #1 - Introduction to GraphQL
Net Ninja
5 GraphQL Tutorial #2 - A Birdseye View of GraphQL
GraphQL Tutorial #2 - A Birdseye View of GraphQL
Net Ninja
6 GraphQL Tutorial #3 - Project (stack) Overview
GraphQL Tutorial #3 - Project (stack) Overview
Net Ninja
7 GraphQL Tutorial #4 - Making Queries (front-end preview)
GraphQL Tutorial #4 - Making Queries (front-end preview)
Net Ninja
8 GraphQL Tutorial #5 - Express App Setup
GraphQL Tutorial #5 - Express App Setup
Net Ninja
9 GraphQL Tutorial #6 - Setting up GraphQL
GraphQL Tutorial #6 - Setting up GraphQL
Net Ninja
10 GraphQL Tutorial #7 - GraphQL Schema
GraphQL Tutorial #7 - GraphQL Schema
Net Ninja
11 GraphQL Tutorial #8 - Root Query
GraphQL Tutorial #8 - Root Query
Net Ninja
12 GraphQL Tutorial #9 - The Resolve Function
GraphQL Tutorial #9 - The Resolve Function
Net Ninja
13 GraphQL Tutorial #10 - Testing Queries in Graphiql
GraphQL Tutorial #10 - Testing Queries in Graphiql
Net Ninja
14 GraphQL Tutorial #11 - GraphQL ID Type
GraphQL Tutorial #11 - GraphQL ID Type
Net Ninja
15 GraphQL Tutorial #12 - Author Type
GraphQL Tutorial #12 - Author Type
Net Ninja
16 GraphQL Tutorial #13 - Type Relations
GraphQL Tutorial #13 - Type Relations
Net Ninja
17 GraphQL Tutorial #14 - GraphQL Lists
GraphQL Tutorial #14 - GraphQL Lists
Net Ninja
18 GraphQL Tutorial #15 - More on Root Queries
GraphQL Tutorial #15 - More on Root Queries
Net Ninja
19 GraphQL Tutorial #16 - Connecting to mLab
GraphQL Tutorial #16 - Connecting to mLab
Net Ninja
20 GraphQL Tutorial #17 - Mongoose Models
GraphQL Tutorial #17 - Mongoose Models
Net Ninja
21 GraphQL Tutorial #18 - Mutations
GraphQL Tutorial #18 - Mutations
Net Ninja
22 GraphQL Tutorial #19 - More on Mutations
GraphQL Tutorial #19 - More on Mutations
Net Ninja
23 GraphQL Tutorial #20 - Updating the Resolve Functions
GraphQL Tutorial #20 - Updating the Resolve Functions
Net Ninja
24 GraphQL Tutorial #21 - GraphQL NonNull
GraphQL Tutorial #21 - GraphQL NonNull
Net Ninja
25 GraphQL Tutorial #22 - Adding a Front-end
GraphQL Tutorial #22 - Adding a Front-end
Net Ninja
26 GraphQL Tutorial #23 - Create React App
GraphQL Tutorial #23 - Create React App
Net Ninja
27 GraphQL Tutorial #24 - Book List Component
GraphQL Tutorial #24 - Book List Component
Net Ninja
28 GraphQL Tutorial #25 - Apollo Client Setup
GraphQL Tutorial #25 - Apollo Client Setup
Net Ninja
29 GraphQL Tutorial #26 - Making Queries from React
GraphQL Tutorial #26 - Making Queries from React
Net Ninja
30 GraphQL Tutorial #27 - Rendering Data in a Component
GraphQL Tutorial #27 - Rendering Data in a Component
Net Ninja
31 GraphQL Tutorial #28 - Add Book Component
GraphQL Tutorial #28 - Add Book Component
Net Ninja
32 GraphQL Tutorial #29 - External Query File
GraphQL Tutorial #29 - External Query File
Net Ninja
33 GraphQL Tutorial #30 - Updating Component State
GraphQL Tutorial #30 - Updating Component State
Net Ninja
34 GraphQL Tutorial #31 - Composing Queries
GraphQL Tutorial #31 - Composing Queries
Net Ninja
35 GraphQL Tutorial #32 - query variables
GraphQL Tutorial #32 - query variables
Net Ninja
36 GraphQL Tutorial #33 - Re-fetching Queries
GraphQL Tutorial #33 - Re-fetching Queries
Net Ninja
37 GraphQL Tutorial #34 - Book Details Component
GraphQL Tutorial #34 - Book Details Component
Net Ninja
38 GraphQL Tutorial #36 - Styling the App
GraphQL Tutorial #36 - Styling the App
Net Ninja
39 GraphQL Tutorial #35 - Making a Single Query
GraphQL Tutorial #35 - Making a Single Query
Net Ninja
40 Build Apps with Vue & Firebase - Udemy Course
Build Apps with Vue & Firebase - Udemy Course
Net Ninja
41 Updated Vue & Firebase Course (Udemy)
Updated Vue & Firebase Course (Udemy)
Net Ninja
42 Vue & Firebase Real-time Chat (Preview) #1 - Intro
Vue & Firebase Real-time Chat (Preview) #1 - Intro
Net Ninja
43 Vue & Firebase Real-time Chat (Preview) #2 - Project Structure
Vue & Firebase Real-time Chat (Preview) #2 - Project Structure
Net Ninja
44 Vue & Firebase Real-time Chat (Preview) #3 - Firestore Setup
Vue & Firebase Real-time Chat (Preview) #3 - Firestore Setup
Net Ninja
45 Vue & Firebase Real-time Chat (Preview) #4 - Welcome Screen
Vue & Firebase Real-time Chat (Preview) #4 - Welcome Screen
Net Ninja
46 Vue & Firebase Real-time Chat (Preview) #5 - Props in Routes
Vue & Firebase Real-time Chat (Preview) #5 - Props in Routes
Net Ninja
47 Vue & Firebase Real-time Chat (Preview) #6 - Route Guards
Vue & Firebase Real-time Chat (Preview) #6 - Route Guards
Net Ninja
48 Vue & Firebase Real-time Chat (Preview) #7 - Chat Window
Vue & Firebase Real-time Chat (Preview) #7 - Chat Window
Net Ninja
49 Vue & Firebase Real-time Chat (Preview) #8 - New Message Component
Vue & Firebase Real-time Chat (Preview) #8 - New Message Component
Net Ninja
50 Object Oriented JavaScript Tutorial #1 - Introduction
Object Oriented JavaScript Tutorial #1 - Introduction
Net Ninja
51 Object Oriented JavaScript Tutorial #2 - Object Literals
Object Oriented JavaScript Tutorial #2 - Object Literals
Net Ninja
52 Object Oriented JavaScript Tutorial #3 - Updating Properties
Object Oriented JavaScript Tutorial #3 - Updating Properties
Net Ninja
53 Object Oriented JavaScript Tutorial #4 - Classes
Object Oriented JavaScript Tutorial #4 - Classes
Net Ninja
54 Object Oriented JavaScript Tutorial #5  - Class Constructors
Object Oriented JavaScript Tutorial #5 - Class Constructors
Net Ninja
55 Object Oriented JavaScript Tutorial #6 - Class Methods
Object Oriented JavaScript Tutorial #6 - Class Methods
Net Ninja
56 Object Oriented JavaScript Tutorial #7 - Method Chaining
Object Oriented JavaScript Tutorial #7 - Method Chaining
Net Ninja
57 Object Oriented JavaScript Tutorial #8 - Class Inheritance
Object Oriented JavaScript Tutorial #8 - Class Inheritance
Net Ninja
58 Object Oriented JavaScript Tutorial #9 - Constructors (under the hood)
Object Oriented JavaScript Tutorial #9 - Constructors (under the hood)
Net Ninja
59 Object Oriented JavaScript Tutorial #10 - Prototype
Object Oriented JavaScript Tutorial #10 - Prototype
Net Ninja
60 Object Oriented JavaScript Tutorial #11 - Prototype Inheritance
Object Oriented JavaScript Tutorial #11 - Prototype Inheritance
Net Ninja

Related AI Lessons

Up next
How to Open HPL Files (HP-GL Plotter)
File Extension Geeks
Watch →