Subtitles section Play video Print subtitles So let's talk about else if statements in PHP. So we talked about if statements and PHP and if else statements and PHP Basically, what else? If statements dio is they check to see if other conditions are true before you default out till toothy else. So basically, with if you may say, if you know dollar sign age is greater than 18 do something else, do something else. But what if you want to check for multiple conditions? So what we're going to show you in the script today is we're going to say only people within a certain age group are allowed to enter a website. So if they're below a certain age, they're too young. If they're above a certain age, they're too old. Else they're allowed to go into the Web page. And so, basically, what else if does it is allows you to query against additional conditions to see if they're true before you default out toe whatever the else light of code is. So with that, let's go over to the computer so I can kind of show you how this works. So this is a very simple script toe look at an age that is given and to say whether or not that the person can enter a website. So basically, is it an appropriate age? Ah, so we've called this else if dot PHP and we've opened up up with the standard PHP tag we have been created the variable age dollar side age, and we have said it to the value of 17 for this first instance. And of course, you do the semi colon. We then do print your age is and the value of age. So again, this is going to show us what the current age is just to make sure that we don't get confused with what the value is. Ah, then we're going to print a break. And again, this is for an HTML. So it breaks toe a new line and the Web browser. And so it's easier to read now past this. It looks like a standard if else statement, but with an additional else if in the middle, so we have. If that we do parentheses, the value for the variable age is less than 18. Then we do this open the squiggly bracket. You know what we're going to say is print you too young and with a semi colon, and they were going to close this out with a closing squiggly bracket. Then we're going to do it. So this is if age is less than 18 you're too young. But we're also going to say is else If so, this is all one word, So it's all written together, and then you're going to do space and the same thing. Parentheses. The value of age is greater than 65 close parentheses. Open, squiggly bracket print. You're too old. So if you're below 18 it's going to print you too young. If it's above 65 it's going to print. You're too old. Then we close with squiggly brackets again. And then finally we have else so else. So if this is true, do this else. If this is true, do this. If neither of these air true than else do this, we open the squiggly bracket that we say print. You can enter, and then we close the spring squiggly bracket. So this is how you can use else if toe add additional conditions. And this is one of the things that makes programming a little difficult for people is trying to figure out how to design if an l statements, if else else if statements how to design all of this stuff. So that actually makes sense and is easy to code. So for here, what we're looking for is, you know what we're looking for is to enter. The positive is you can enter. But in order to get to the positive, you have to make sure they're not too young and make sure they're not too old. So tryingto layout, these kind of statements could be a little bit confusing to people. And it's Mauritz more the logical issue. Actually, writing this obviously is easy, but it's a logically Where do you position things in the if else if and l statements that could be confusing. And then finally, what we dio is we close out the PHP and it's done. So this is all saved. We go to chrome, we go to silicon dough, Joe. We then go to else if and so your age is 17 so that shows us what the value of variable is. And so it says you too young. Now if we go here and we changed this to Let's say 66. We do file. We do save we g o else. If we upload again, you appear we refresh now says your age is 66. You're too old now if we want to make it, Sure, it's just right. We go in, we plug in 43. So 43 is above 18 and 43 is below 65. So if everything is right, it should print that were able to enter, float again and then we refresh your age is 43. You can enter. So that's basically how and if else, if statement is written, you can put multiple else ifs in here so you could have won if another than an else, if that another else if that another else If. But you do have to be a little careful with how you do do that because they can get very confusing very quickly. So that's all there is to else. If statements and PHP it looks like a normal if else statement, you just basically add additional questions in there. Is this true? Well, how about this? Well, how about this? Well, how about this? Well, what about this? Right? And it keeps going through until the condition is true, and then it does whatever that is that that you have the code in there. So this is basically multiple ways. Query to see what a condition is to see if something should run. So again, like with this, if you're below a certain age, then you shouldn't be able get in. If you're above a certain age, you should be able to get in. You can only get in if you're in the right space. Now the main things that makes else if statements confusing is not actually writing and LCF statement. As you can see, the code for an LCF statement is really simple again. If something is true, then everything between the squiggly brackets runs. So whether that's dumping information into a database, whether it's sending out email is in the mail function. Whether it's printing out something on the screen, you could have one line of code. You could have 1000 lines of code. But realistically, that's actually rather simple. What gets confusing for people is how to write the else. If statements in the proper way, like are you, you know, there's there's there's ways that people think about? The question is, is, Are you between this and this? And some ways in order to see are you between 18 to 65? One way you can ask that question is, are you younger than 18 and are you order than 65? And if both of those air negative than by by default, you are within the proper age group. So the main thing is that makes these kind of statements. Confusing isn't actually the statement itself there the code, it's Maur the mental gymnastics you have to go through to figure out what you're really trying to ask and how you can get that information out of that code. So that's all and else if statement is and that's why it matters.
A2 age squiggly php print statement bracket PHP - elseif Statements 2 0 林宜悉 posted on 2020/04/06 More Share Save Report Video vocabulary