Placeholder Image

字幕表 動画を再生する

  • So let's talk about if statements in PHP essentially all an if statement is is is a line of code that states.

  • If a variable is something or is not something, then do something.

  • So if a variable is above a certain amount, then trigger and action happen.

  • Or if a variable is below a certain amount trigger in action happen.

  • So all this is doing is this is taking a look at the variable.

  • It's comparing the variable against something, and then, if that is true, then it's going to trigger an event toe happen.

  • So that's all an If statement really is.

  • Let's go over the computer so I can kind of give you a demonstration of an if statement.

  • So this is a very simple if statement in a PHP script to give you a demonstration of how this works.

  • Do understand that this can be a much more complicated.

  • The events that could be triggered could be much more complicated, but this is a simple way to show you how.

  • If statement works so again, what we do is we open up the PHP with a PHP tag to say a PHP script is going be run.

  • And then what?

  • The first thing that we're going to do here is we're going to create an assigned A value to a variable.

  • So we're going to create a variable called dollar Sign age and we're going to set it to the value of 16.

  • And, of course, always remember the semi colon and the first thing that we're going to do here when you're first starting when you're playing around with if statements, wild statements while loops, that type of thing.

  • One of things that you're going to want to make sure is that the variable that is being compared is is the value that you think it's supposed to be.

  • So one thing that happens with a lot of coders as they go in, they write a lot of code.

  • You know, if if the variable is this value, do this so on and so forth.

  • But then sometimes they screw up and they value of the variable isn't what they think it's supposed to be.

  • And so then the if statement doesn't seem to work properly because the value that's being being presented to the if statement isn't what people think it is.

  • So the first thing that we're going to do here is just verify what the value we're sending to the if statement ISS so we're going to do is ready to print double quotation marks and then we're going to do your age is so this is all printed out, a normal text and then by simply putting dollar sign age or whatever the variable name is that will print out the value for that variable.

  • Then we're going to close with a double quotation marks.

  • So this is going to say your age is and hopefully it will spit out 16.

  • If it spits out something else, then we know.

  • Oh, there's an issue going on with this code.

  • Then we're going to do semi Colon now again, since we're going to be printing this out into a normal Web browser that reads HTML.

  • We want this in a format that we can read that's relatively easy to read.

  • So we're going to do print double quotation marks and then we're going to simply put in the break tag.

  • So all this does is four html.

  • It will print this out on one line, Then it will see the break break tag, and then it will print out everything else on the next line.

  • So it's not all lumped together in one single line.

  • And then this is where we get to the if statement.

  • And so this is a very simple if statement here.

  • And so for an if statement, what you do is you open it up with And if you say, if you do space that he do open parentheses and then you put in what the variable is or what what you're you're tryingto test against and so we're going to test against the age.

  • So the value of the variable age if the valuable of the value for the variable age is less than 18 so we could make this less than 18 hopes we could make this greater than 18.

  • We could make this equal to 18.

  • We could put a lot of conditional Sze in there.

  • We'll talk about conditions later, but right now we say if the value of age is less than 18 that would close parentheses that we opens squirrely brackets always remember the squirrely brackets.

  • So basically what's gonna happen is if this is true, then do everything between the squirrely brackets.

  • So here all we're going to do is print.

  • But you could you could have 100 lines of code between the squirrely brackets.

  • You could have a lot of stuff going on, but all we're going to do right now is where it's gonna print.

  • We're just gonna sprint a simple line double quotation marks.

  • And we're just going to say you're you too young.

  • You too young.

  • You know you're too young for the ride or you're too young to get into this website.

  • Whatever else you can print whatever you want here, then of course, we're going to do the semi colon to close that line.

  • Then we're going to close the squirrely brackets and then we're going to close the script altogether.

  • So basically what this is going to dio as you plug in the age, it's going to print out what your age is.

  • It's going to do a break.

  • If the age is blow 18 it's going to print you too young.

  • If the aid is above 18 though it's important.

  • Understand?

  • All of this is going to do if it's if the age is below 18 it's going to print out this line.

  • If it doesn't, if the condition isn't true, then it's going to simply keep going on with the strips script, so it's not going to do anything else.

  • And so this is all basically basic.

  • If statement and then we're going to open up Chrome, we're going to go to silicon dodo dot com again, This has been named, if not PHP.

  • So again, remember has been named out PHP.

  • Then we're going to hit enter, and so is going to say your age is 60.

  • Okay?

  • And so the big thing to understand here is that we're just looking to make sure that the value of the variable is what we think the value of the variable is.

  • A lot of times people get messed up in programming because they're trying to keep so many things in their head, and they just make a small little mistake.

  • So we're going to make sure Okay.

  • Yep.

  • So the value of age is 16.

  • And so since the value of ages 16 is going to print out, you too young.

  • Now, if we go over here, all we have to dio is if we change the value of age to, let's say 20.

  • When we do file, then we do save.

  • We're going to grab the if dot PHP script.

  • We're simply going to upload this to the server, so it's now been uploaded.

  • We go back to Google Chrome.

  • All we do is we hit, refresh, and now, since the age is 20 then nothing happens.

  • That print that print is not triggered.

  • So that's all there really is.

  • No.

  • What if statement is?

  • The thing that you're doing is you're trying to test for conditions.

  • So if the value of a variable is less than greater than equal to whatever else the value of something else, then do this.

  • If this is not true, if this is not true, then this anything between the squirrely brackets is simply skipped over.

  • Nothing else happens, and the rest of the script is run.

  • So that's all there is to.

  • If statements now there's a lot of ways for testing conditions.

  • There's if statements there's if else there's if else's with else ifs, there switches.

  • There's a lot of ways to test for conditions.

  • But using a basic if statement is one of those ways one things that gets a lot of new programmers compute confused as they see the if else's and the if else's with else ifs.

  • And they think that they have to do that every time that they're going to try to test for a condition.

  • So it's important.

  • Understand that you can use a simple, if statement on its own.

  • So, basically, if if a chunk of code is supposed to run, if one thing is true, and if not, it doesn't need to run that you could just basically put a simple if statement in there, you don't have to put in additional else.

  • You just have that.

  • If statement, if this is true, whatever it iss ah, then do the rest of the code.

  • If not, then that code isn't run.

  • So this is one of the things that you can do to test four conditions with in your code.

  • So that's what an if statement is, and that's why it's important in PHP.

So let's talk about if statements in PHP essentially all an if statement is is is a line of code that states.

字幕と単語

ワンタップで英和辞典検索 単語をクリックすると、意味が表示されます

A2 初級

PHP - if ステートメント (PHP - if Statements)

  • 1 0
    林宜悉 に公開 2021 年 01 月 14 日
動画の中の単語