IF…ELSE Statements

Hey guys, welcome to this new video where we’ll be looking at if statements in PHP. Now picking up where we left off with our index page, we will just want to make our index page. Like I said, it’s the first page you land on. So it’s our landing page. It’s our home page. So from here, we’re going to add a few anchor tags that will allow us to navigate to all the other sub files that we have for this lesson. So I have added a few courses or a few pages rather to go alongside each lesson for this course. So for this section of the course we’ll be looking at arrays for and while loops, and if and switch statements is for if statements but let me just give you a bigger picture. When it comes to programming. We talk about these things called control statements and there are three types of control statements. You have sequence, then you have selection. And then you have repetition, not necessarily in that order, but the one that’s absolute is always sequence, meaning whatever sequence you write your code in, that’s how it will be executed. That’s the general idea of the sequence control structure. So just like with printing this page, we wrote the h1 tag first. And the expectation is that it should be at the top of the page, and everything that we write in the order that we wrote it, it will be executed accordingly. For selection, it means that if you have to make a decision or choice between two options, two or more options, then you have to make a selection or a decision. So that’s the next control structure. And that is the control structure that the if statement represents so so I have gone ahead and set up an h2 tag with some list items. And these list items are really just oncotarget Still different pages that we’re going to create here in our site. So already you can see how we can go both stringing together a niche PHP site if you’re used to hold to do it with a static HTML is the same principle with a PHP. The only difference is that each page should be ending with dot php. So the next page that we’re going to create in our site is the if statement dot php. So back to the Explorer pane, I can just go ahead and create a new file. And then I’m going to name it if statement dot php. And there it is created. The next thing I want to do is just bootstrap this html5 boilerplate template. And I’m going to give this piece of named PHP, primer, hyphen if statements. Now in programming, there are quite a few times that we would probably want to make a decision and one easy way would be to say it If somebody passes or fails based on their greed, now that would be we’re doing PHP will carry out every action instead of a PHP tag. So I’m just going to spin up my PHP tag from here. And every bit of code that I write everything that I want to go on the page, I will do from between these tags. Now, I’m just going to put on a comment and see if these we’re just going to be looking at if statements. And the first thing that should be printed to the page is an issue tag with the words if statements. So we know that whatever we echo should be rendered on our page in current HTML according to how we wrote it. Now let’s try something simple. let’s declare a variable here in our PHP tags and call it greed. And I’m going to assign grades to have the value 50, which is generally the past mark in most institutions. Now we want to look at how Could printouts read text if the person has failed, or green text if the person has a passing mark. So in PHP, the if statement looks something like this, we read the word if, then we open brace and Visual Studio is automatically closing it for me because they don’t want me to forget to close it later on. If your editor, if you’re not using Visual Studio code, and your editor does not do it like this, then make sure you close it first, so open and close at the same time, so you don’t forget down the road. We’re all human. And then what I do after opening and closing is type the variable so the variable is going to come under scrutiny. And then I know I need to decide under what circumstances will I take what action so I have my variable which implicitly has a value and then I need to compare it with something so I can make a decision based on that comparison. In this situation, we did say that If the passing mark is 50, then anything over that that’s our over is our pass and anything that’s our well, anything under is a failure. So I’m going to see if the variable Greed has a value that is greater than, and we’re using the greater than sign or equal by using the equal sign, and then I give a comparable value. So inside of these braces, I’m going to see if set variable is meets this condition with this value, right? So the condition is greater than or equal to some value, then and well there is no word then at least not in PHP. So we open and close curly braces this time. Alright, I’m just going to break a line in between. So we have the parentheses or regular brackets that we all right and used to love in the English language. And then we have the curly braces which will be We use less frequently in English, you know writing but very frequently in coding. So we open the curly brace. And then inside of those curly braces, we put whatever actions you want to carry out. So I’m just going to keep it simple right now. And I’m going to say echo. And I’m going to echo an H three tag, and I’m going to see you have passed. Alright, so we’re not good at setting the colors. I’m just echoing something simple. Just to demonstrate this if l selection, so we have if and a condition and this condition will contain some variable or some value on the left. Some comparison operator and comparison operators can be another three of them. He can be equal, equal meaning equivalent. It can be greater than it can be less than it can be. greater than or equal? Well, I really know which one is which one is greater than or less than, but the fact is you can pair them. So whichever one you are using, you can pair with an equal sign. All right, so you can look if it’s exactly the same, I think in PHP have a triple equal sign, so I just put that here. So that way you can compare if they’re the same value and datatype. This one looks at the value if it’s greater than less than or less than an equal greater than an equal. So if this condition is true, if if this comparison is the one that works, because green is greater than or equal to 50, then we will echo the sentence you have fast and then we can see else to give an alternative to see meaning if this is true, then they will do it. That, if this is not true, then do this. So it’s just like, if it is raining, our Lord said, otherwise I’ll stay inside. So you know, this would be that one condition that determines what you will do. And if that condition doesn’t hold true, then you do something else. That’s what if else really means. So here, I will just echo you have failed. So I’ll just copy this code, you know, work smarter, not harder. And I’ll see you have failed. Now, let’s browse to this page and see what the outcome of this bit of code is. I’m just going to go back to my browser where I already have my Index page. And I’m going to refresh because I made some changes since the start of this video. And here we see we have all of those links, as we outlined in the Index page earlier, and one of them is the if statement, so I can just click this and it will browse to the if statement page. I noticed the change in the link. It goes from PHP parameters. So PHP premise slash if statement dot php. And then once we land on that page, we see you have passed. Now if I change this grid to 30, then we should see something else being printed. So I changed agree it’s a 30, save that change, refresh, and then it’s telling me you have failed, all right. So we can tell that it is making a decision it is comparing whatever value is inside of this variable with this variable to see if it is greater than or equal. If it is, then it prints you are past and if it is not, then it says your field. Now, we said we wanted to print in red, or green. So I’m just going to add some CSS right here inside of this tag. And I’m going to say, style equals and then if in case you’re not necessarily familiar with CSS, this is what he called inline CSS, where you use the style attributes. He works in regular HTML. So If I wanted to do that, I could say H three, open or sorry, I can do that inside of PHP. Sorry, let me do that above H three, and then open and close H three. And then if I wanted to make this red, I would just go on to the issue type process space between the tag name and the angle bracket, and then type the attribute style. And then make it equal to some value, some selector and value pair. So the selector value pair here for color would be literally color. And I would say, red or green or blue, just to keep it simple. So the same way I can construct this in regular HTML, I can actually echo that in the PHP. So I’m just going to copy this part since that’s the part I have outstanding. And I’m going to paste it here. And notice I can use the double quotation marks inside of the single quotation marks. But if you mess that up, and you use single quotation marks here, then it can cause problems. And you’ll have to end up contract anything and complicating a relatively simple situation. So just be careful. If you open with single quotes and you need quotes inside use double quotes and vice versa. So I’m going to add that to this. So this should print Well, this should print green since you have passed. And if you have failed, then it will print the same h3 tag in red. And then let’s try that again. So I’m going to refresh this page. And we’re still on the failure value. So you see that it has turned to red. And then if I drop this to 80, which is greater than or equal to 50, then it should print in green. All right, so that is how you know you’re on the websites. And you see that dynamism between whatever you put in and the color schema that you get back kind of visual cues. This is all they’re doing. They’re just seeing, if you put in a certain value that we don’t like or we don’t approve off, then display her. It takes If we approve or within display green text, that’s all it’s saying if some condition else. Now if we add three conditions, let’s say we had three tiers of greeting, we can see if then else I just read if, else if. And else. All right. So what if you had three or more conditions that you needed to come here, like, for instance, you had, maybe you wanted to print on the little grid based on the grid that a person got. So I can read the clear grid right here. Once again, these variables are not strongly typed. So I can easily declare it as a string this time. So it’s a number up here, and it’s a string down here, and once again, sequence rules. So it starts off as a number, then it does all of these operations as a number, but then once he gets here, he realizes Oh, my master wants me to change to a letter so it becomes a letter and then we start to Some operations don’t hear we think does a letter. Now this is not necessarily recommended. I’m just doing this for example purposes. What PHP is that flexible? Now let’s get to work. So we will say if and that syntax is pretty much to see him doing it. Sometimes it doesn’t make sense to ask why you have to put in the braces just know that if you don’t, it doesn’t work. That’s just the rule that the programming language abides by. So we say if open brace and then our condition which is greed. And let’s see if it is equivalent to an A. Then we print and then there are times when if you’re doing one line after the if statement, they don’t need equal to your curly braces. I recommend them though because if you indentation expand the code later on they I mean they’re already there. Right? So no harm no fault. So if greed is equivalent to a, then I want to print Oh, it’s an h2 tag. And I’m going to say you are a super star, exclamation close. I will come back and deal with the colors later on. So then we’ll close that. And remember, we end every line with a semicolon. If you don’t, then you end up with some problems with some curly braces, some red line, sorry. And if I refresh this page, then it will tell it I have a syntax error. It won’t necessarily spell Oh, you’re leaving off a semicolon on this line, but it will just express it I have an arrow. So this remember your semicolons that Remember to follow the rules of the programming language. So I can say if the grade is equivalent to a then your prints that you’re a superstar else If, and then we specify another condition, which I could understand to copy this condition. It could be else if the greed is equivalent to B, then we can still motivate the person. So I’m going to see Echo, sorry, Echo, and when to echo in an HTML tag each to this time again, and see you did well, exclamation side. So it’s still encouraging the person. But then if they got to see or any grade, now below that, and then of course, these conditions are based on what values you know will be there because if the person got an A plus, then the else would not detect that Oh, it’s near. And so you know, you have to call it according to your context. If I’m just doing a B, C, D, E, F, I know that there’s no a plus or B minus or anything like that. Then I can do this, but then if I wanted to send the same message for different tiers then I have to add on operators like and and then see another conditions I could see and greed or I can see rather are sorry greed is equivalent to another value, then it will say if it is this value or it is that value then do this alright. So, you know for your context you will be very particular about how you write your if else if, and else statements, but every else if else statement must end with an else because the else is the catch all it means that, if nothing matches these two conditions, then it must much just catch it and do something. All right. So, then if anybody got a C in this class after this good today Then we will say you have failed. Alright? And just let them know nice and gently that they didn’t pass the course. Alright, so they either got an A, A B, and they passed or they failed. And then if I run this code with a, then I should see this refresh. You are a superstar. Alright. If I change the variables value to be and rerun save and then rerun, then I will see a different message and just the same if I put anything that’s not an A or B, then it is going to print that you have failed All right, and then just add a bit of color to it. We can just copy the styles from above. So you are superstar brings in nice green, and then I just use that same status same code is just the value that changes. So if you have if you did well you get blue text, and then if you fail, then you get red text. Alright, so if you are superstar, you get green. If you did well you get blue, and then if you fail, you get red. And then I can just reprint that so you have failed to print with red. All right, and then if you can go ahead and experiment and change the values and expand the list to account for more than one more than three sorry values based on the grading scheme that maybe you pass through the college you went to with and then you will see how dynamic this if else can make your code. Alright, thanks for sticking around and if you have any difficulties or any questions, feel free to drop me a line at and I’ll be sure to reach out and help