Subtitles section Play video Print subtitles So let's talk about if else statements and PHP. So basically all in. If else statement does, is it tests to see if a condition is true? If it is true, it runs a certain section of code. If it's not true, it runs a different section of code. So if you're going to see if variable equals, a certain amount is less than a certain amount is greater than a certain amount. If that comes back, is true. Is going to run one section of code. If it comes back as false, it's going to run a different section of code. So this is where we call Colin, if else statement. So if this is true, run this code. If it's not true, run a different section of code. It's pretty simple to actually run in the real world, so let's go over to the computer so I can show you a demonstration. So this is a simple example of how if else statement works, it is important to understand that as far as the conditions are concerned, and as far as the events that are triggered, this could be much, much more complicated. You can have 100 lines of code after the if if you want something complicated to run. But right now all we're going to do is we're going just print and output. So when we create this PHP script again, we're going to open with PHP tag called PHP and say, We're about to run a PHP script here. We're going to create the variable age so dollar sign aid and we're going to set the value of age to 19. And of course, we're going to end with a semi colon. Now what we're going to do here is just so weakened view. Since we're new coders, we want to make sure that the value of the variable is what we think the value of the variable is. So what we're going to do is we're going to print out on the browser. You're going to print double quotation marks your age, and then you just simply put dollar sign, age or whatever. The variable name is here and the new clothes with a double quotation marks and end with a semi colon. So what this is going to do is is going to print out your age is and currently your ages 19. So it should say your ages 19. Then we're going to print. And since we're gonna be looking at this in a Web browser, we need to format it so it doesn't look absolutely ugly, so silly. So it makes sense to us. So we're going to do is we're gonna print double quotation marks and we're going to simply put the brake tag again. So it's going to print out your ages 19 and that is going to print break. And so what that's going to do is go in. It's going to break in html, toe a new line so the content will show up on at least two lines. Then we're going to come down here to the if statement or the if else statement. And what we're going to say is, if open parentheses so that the value of the variable age, So if the value of the variable age is less than 18 close parentheses, open squiggly brackets, and what we're going to do is we're into Prince double quotation marks. You too young. So basically what this is saying is, if you're under 18 then it's gonna print out you too young, right close double quotation marks semi colon, of course, of course, and they're going to go to the next line, and the first thing they're going to do is close the squiggly brackets. So you open the squiggly bracket here closes squarely bracket. So it's important. Understand, is if if this is true, then it's going to fire off everything between these squiggly brackets. So again, whether you have one line of code, whether you have a 1000 lines of code, it will keep trying to run until it sees the end of a squiggly bracket. Then, once you once you close a squiggly bracket, what you're going to do is in print else. So if this is true, do this else. So if it's not true, do a space open, squiggly bracket and then again do everything between the open squiggly bracket and the clothes squiggly bracket. So what we're going to do is print prints, double quotation marks. You're old enough closed double quotation marks, semi colon in the line, and then we close a squiggly bracket. So this is a very simple thing. So let's say like an age verification website. So right, if people are under a certain age. You want to restrict access to the website. If they're over a certain age, you want them to go to Gettinto into a website. So what you could dio is if the ages under 18 you could simply print you're too young and then print out a whole terms of service. If you wanted Thio else, if they're over 18 you could print out so you could actually have PHP dynamically print out the log in for rice, the log inform username, password so on and so forth. So if it asked for the age and they're too young, then they're told they're too young. They're spit out. If it asked for the age and they're old enough, then it can have a long and form or could have a sign in form or you print out whatever it is you want to print out. So that's all gets done, and then it closes the squiggly bracket, and then at the end, we're going to close a PHP tag. So this is basically all it is, too, And if else statement, then when we go to Chrome and we actually run this so we do Silicon Dojo and we go to, If else so it's going to say your age is and so is going to give. The value of the age variable, which is 1919 is obviously above 18 and so it's going to say you're old enough. If we go back and we change this toe, let's say 15. When we do file that we do save, then we upload this to our server again and then we go here and then we refresh Now your ages 15 you too young. So this checks against the value of the variable. If it is true that it runs one set of code else, if it's false, it runs a different set of code. And so that's really all there is to an if else statement. So that's what if else statement looks like and how you use one now. It's important, understand. In PHP, you can check on the condition of a value of a variable or a value in many different ways. You can use if statements you can use. If l statements you can use if else else. If statements you could use switches. So if you're sitting here and you're thinking well, wait a minute. Okay, so if it's true, do this and it's false. Do that. But what if what if you have, like, 10 different options? That's where you would use something like a switch in order to to check on the condition of a variable or the value of a variable on. And that's something that will teach later. So basically, with if else all we're looking at, if is if it's true, then do one thing else. If it's false, do something else. So that's all there is to it if l statement, and that's why it's important in PHP.
A2 squiggly print php variable bracket quotation PHP - if else Statements 4 0 林宜悉 posted on 2020/04/06 More Share Save Report Video vocabulary