Placeholder Image

字幕列表 影片播放

  • So let's talk about the comparison operators and PHP.

  • So if you're going to be doing if l statements or if you're going to be doing loops, you need to test the value of a variable against something to decide what should happen next.

  • So is the value of a variable equal to something?

  • Is it greater than something less than something not equal to something less than or equal to something greater than air equal to something?

  • Basically, this is what's going to decide whether a loop should continue or what should happen in and, if else statement so comparison operators are basically just a very simple concept.

  • This is very simple.

  • Part of Matthew probably learned when you were about eight, and all you're doing is you're asking is the value of a variable equal toe a value?

  • Is it not equal to a value?

  • Is it less than the value is greater than the value?

  • Is it less than or equal to the value, or is it greater than or equal to the value?

  • And if that is true than the rest of the code will run or the loop will continue?

  • So it's all we're really talking about with comparison operators with comparison operators.

  • This is one of those.

  • This is one of those reasons I said yes.

  • People buy a book now again, if you like the Internet, you don't like books.

  • Me is an old timer.

  • I like a book because, as you can see here, when you start looking at comparison operators and also something called logical operators when your new it's very easy to just be able to open up to a page and have everything listed here.

  • One of the big issues you want to run into with new coders is that they think they imagine that career.

  • They imagine that they could memorize everything, and then they screw up some little line of code, and they spend the next three hours trying to figure out one little screw up.

  • Then they get frustrated and they quit.

  • So is simply being able to have feet sheets like this when you're riding, and if for a loop or something like that and just making sure you're writing things, how they're supposed to be written, that can make your life a hell of a lot easier.

  • So with that, let's go over to the computer, and I can give you a demonstration of how these comparison operators work.

  • So this is a very simple comparison operator script.

  • Basically, this is just a way to say hello to somebody if they come to your website and make them feel as if it's a little personalized and then decide based on their age, whether or not they're allowed enter.

  • So in this, obviously we opened up with the PHP tag, and the first thing that we're going to do is we're going to create a dollar sign name as a variable.

  • And for the first test, we're going to set it to Bob.

  • Now, when you set a variable in PHP, it's important that you use the double quotation marks.

  • And then, of course, you close with a semi colon were then also going to create the vow, the variable age, and currently we're going to set that value to 20.

  • So these are the two variables that we're going to be using for this particular script.

  • Now, what we're gonna do is we're not simply go into a comparison, so we haven't and else if statement here.

  • So this is an if else If else statement.

  • And so what we're going to say is, if name, dollar sign, name equal equal.

  • So if it's two equal signs, it means is it equal to remember if you only put in a one equal sign that's going to try to set the value of the variable to what it is?

  • So one equal sign will set the value of a variable to equal signs, questions, whether the two are equal.

  • And then when we're going to be doing this, whether it's a loop or whether it's in the F statement, we're going to use a single quotation mark for a string and we're going to put in Bob.

  • So currently the value of name is Bob.

  • And so down here, we're going to ask, is the value of the variable.

  • Bob got close with a single quotation mark.

  • Close parentheses, open squiggly bracket.

  • So if this is true, so with if an else if statements, the question is, if this is true, then open squiggly bracket print.

  • And then hello, sir.

  • So just a normal Hello, sir.

  • So, basically, if it's Bob, we're going to say hello, sir.

  • We're going to then close the squiggly brackets we're then going to say else.

  • If so, an additional if right and then we're gonna open parentheses.

  • Dollar sign, Name again.

  • Again equal equals a two equal signs.

  • So if name equals Sue, remember single quotation marks here and we're gonna close parentheses, Squiggly bracket.

  • Then we're going to say print.

  • Hello, ma'am.

  • So basically, it's Bob.

  • We're going to say hello, sir, If it's Sue, we're gonna say hello, ma'am.

  • Else, since we're doing an else else if else statement else if it's not Bob or Sue where we're gonna print, who are you?

  • So that's going to be the Fail out if if those aren't true, where they're going to come down.

  • So this closes out with squiggly bracket were then they gotta come down and we're going to simply print a break.

  • So we print the break.

  • This is for HTML.

  • So in an HTML, it will put that on one line in the next thing on the next line and then down here, we're going to do a comparison operator for a number.

  • So we're going to say if they say the value of the variable age is greater than or equal to 18 now it is important that you put these in the right position so this greater than or equal to or less than or equal to will work equal to or greater than or equal to and less than will not work.

  • So the greater sign or the lessor sign goes first and then the equal.

  • So if age is greater than or equal to 18 and this is important because if I simply put greater than 18 what does that mean?

  • That means 19 and above right.

  • 18 is not greater than 18 right?

  • They're equal to.

  • So if I said if age is greater than 18 that means in order for this this if statement run, it would have to be like at least 8.1 right.

  • It would have to be greater for it to run.

  • So what we do is we say, greater or equal to 18 and so that gives us the 18 year old cut off.

  • So this is important to be thinking about when you're writing the scripts.

  • You know, if you have a, let's say, an age of 21 cut off.

  • So again, for an adult side, a a poor inside or something, you know?

  • And so you have to be 21 in above Tiu to look at it.

  • If you simply put greater than 21 then that means 21 year olds will not be able to see it.

  • That they would have to be 22 because 22 is greater than 21.

  • So important thing to be thinking about these comparison operators.

  • Then all we're gonna do again, standard, if else squiggly s O.

  • If the A the value of age is greater than or equal to 18 we're going to print.

  • You can enter else simple else print.

  • You cannot enter, and then we're going to close the squiggly brackets, and then we're going to close the PHP, and that's the script.

  • So basically, when we're doing a comparison against the number, so a number value 18 18.88 whatever, you don't have to use any quotation marks.

  • If you're going to be testing against a string, so a name or a sentence or something like that.

  • You used the single quotation marks.

  • When you run this, then we go over silicon does Joe, and we do slash comparison PHP So says Hello, sir.

  • You can enter.

  • So if we look, the value of the variable name is Bob.

  • So if the value variable name is equal to Bob, you're going to say hello, sir, His age is 20.

  • So age is greater than or equal to 18 says you can enter.

  • So if we go here and let's say we changed the name to Sue So we're going to say the name there Person's name is Sue and their 15 years old we're going to save.

  • We're then going to upload.

  • They were simply going to refresh, so hello, ma'am, you cannot enter.

  • So says hello, ma'am.

  • Because the value is now sue so else if the value equal sue the ages 15 So the ages under 18 So you go to the else So it says that you cannot enter.

  • If we do here we say, Ah, I don't know.

  • We just put in George again.

  • We put in 25 Then we put save, Then we upload and then we refresh.

  • And now it says, Who are you?

  • So you're new?

  • You can enter.

  • The reason being is the name the value of the name George is not bob, it is not sue.

  • Therefore, defaults fails out to else ages 25 age is greater than 18.

  • Therefore, it says you can enter.

  • So this is basically how the comparison operators work in a very simple script.

  • That's basically all there is to comparison operators and PHP.

  • All you're looking for is there's the value of a variable equal, not equal, whatever something else.

  • If that is true, then an event will be triggered, something that will happen now.

  • It is important, understand, since is a bit again, er, level PHP class that there are farm or comparison operators, so you can get a lot more complicated with this.

  • One of the problems that that you run into, though, is that I think people can get very confused when when new people get beyond, equal to or greater than less than or not equal to, you start running into an issue where they star.

  • They start questioning like, what's the difference between equal to an identical?

  • And here's the thing right.

  • At this point in time, you don't you probably don't need to worry about what the difference is between being the same.

  • First is being identical.

  • That's that's kind of a technical thing, which does become more important, as you do Mork complicated programs.

  • But right now equal to should be fine.

  • You probably don't have to worry about identical now if you are interested in Maur of these operators, and I don't want to have to write a by a book.

  • If you go to PHP a dot net, they do list out these operators.

  • I can show you over the computer real quick so you could do a simple Google search for PHP dot net and then look up comparison operators.

  • And that's when you get into this whole list of different comparison operators.

  • And to be clear, if you are new, I would not suggest you get too worried about these.

  • This can make you very confused needlessly.

  • So as I talked about with, uh, with the demonstration we have equal to so equal to equal, Thio means equal.

  • So is one eagle to another.

  • But if you have equal to equal to equal to, that means identical, not is known.

  • Not only is the value the same, but the data type is the same.

  • What that means is something we'll worry about in a different class.

  • And so that's where I get worried when you start looking at too many of these things.

  • Ah, one of the things that I didn't show you and the demonstration is not equal to sew.

  • All not equal to is is if you do the exclamation point equal, that means not equal.

  • So if dollar sign, age is not equal to 20.

  • So then do something, right, So that should be triggered.

  • Ah, Then you have a lot of this other like weird stuff, right?

  • If you have less than beside a great size beside a greater than sign, you could get a knotty not equal to after type of juggling week again.

  • This can be important later, later Life again, Not identical.

  • But then we have less than here.

  • We have greater than here again, less than or equal to or greater than equal to.

  • So this is very important and coding.

  • Everything has to be written perfectly.

  • So it is less than or equal to or greater than or equal to.

  • If you put the equal sign before it just won't work, right.

  • And then you come down here and you have this spaceship thing or whatever.

  • So these are the comparison operators, and this can give you a better idea of some of the other comparison operators something for you to be thinking about.

  • If you're walking this particular video, I would stay very simple, equal to and not equal to greater than less than equal to or greater are greater than or equal to or less than or equal to.

  • I would stick with those right for the second.

  • Why not worry about the others right now?

  • And so there is the comparison operators and those air how they worked again?

  • A very simple thing, but a very important again if you're gonna be doing if l statements, if you're going to be doing loops, you have to be a bit.

  • Compare the values of variables and make sure that the script runs based off of what those values are.

  • So that's all that there is the comparison operators, and that's why they matter.

So let's talk about the comparison operators and PHP.

字幕與單字

單字即點即查 點擊單字可以查詢單字解釋

A2 初級

PHP - 比較運算符 (PHP - Comparison Operators)

  • 7 0
    林宜悉 發佈於 2021 年 01 月 14 日
影片單字