Placeholder Image

字幕表 動画を再生する

  • So let's talk about the switches statement in PHP.

  • So we talked about if, if else and else if statements.

  • And so these are all ways that you can look to see if a condition is true and if it is, then you can run code.

  • The question is what happens when you start getting really, really, really big else.

  • If statements you know okay, you have an if in an else, if another else, if in another else if and another else it can start to get very confusing and very difficult to read very quickly.

  • And that's why many people decide to use a switch statement instead.

  • So basically what a switch statement does is it looks to see what the value of a variable is and then based off the value of the variable than whatever the cases or the switch.

  • It will then run the code that's assigned to it.

  • So basically, this is a way you can say that if the value of the variable is this and then do this.

  • If it's this than do this, it is this that do this.

  • It is this than do this.

  • If it's none of those things you go to what's called default and then you run whatever code you put under default.

  • So it's away.

  • It's a way of, ah, looking at things.

  • Let's say you have radio boxes or combo box is.

  • Basically somebody puts in an option.

  • So it let's say you scroll down a list and so you can you can have a list of different states.

  • And so if somebody picks Michigan, then you can go to a switch and basically says, If the case is Michigan than what you do is, you run this line of code.

  • So this is where you have known variable values, like many known variable values.

  • And then it can quickly take a look, see what the value is and then based off of what that value is, Nick, and then run a certain amount of PHP code.

  • So with that, let's go over to the computer so I can give you a demonstration of basically how this works.

  • So this is a basic script to show you how a switch statement works.

  • So we've called the switch dot PHP course.

  • We open with PHP tag and then for this.

  • Basically, what I'm thinking about is the length at the time to ship a product to different regions.

  • So let's say you go to ups dot com or fedex dot com, and it tells you what region you're in.

  • And then it tells you how many days to expect the shipping to be.

  • So with that, that's what I'm kind of thinking in this particular scenario.

  • So what we're going to do is we're going to create the variable dollar Sign region.

  • So this is what we're going to querying against.

  • We're going to see what the condition is, and currently we're going to set this to region one.

  • So we're just going to set this to Region one and then, of course, close with a semi colon.

  • Then we're simply going to go into the switch statement.

  • So with how the switch statement works, is used the switch.

  • So you say swick than open parentheses.

  • And then you dumped the variable name in so whatever condition you're looking for from a variable, you put that variable here and then you close the parentheses, then passed that you open the squiggly brackets and then this is where we go when we take a look to see what we should do, based on what the value of dollar side region ISS and so one of things you'll notice is there's no equal signs here.

  • There's no greater than or less than signs here.

  • Basically, all we're doing is we're creating Quirino against the value.

  • So we're going to say case and then if we have a numeric value such as one, we don't have to use any double quotation marks or single quotation marks.

  • So here we have case, and so for case one.

  • So if the value of region is one which it currently is, then you're going to do a colon.

  • So a full fledged colon from here.

  • What we're going to do is we're then going to write out whatever PHP should run if case is equal to one.

  • So with this, we're just going to a simple print double quotation marks one day, shipping closed, double quotation marks, semi colon, as always, and then the important thing to remember with switches is for each one of these cases, you have to end it with a break.

  • You're going to say break.

  • This is where you break out of the switch and then the semicolon, and what happens here is this will break and then that will.

  • That will leave this whole, this whole switch statement.

  • Then we're going to case.

  • And so, for this case, if it's the value is to the value is to then we're going to do Colon.

  • And then we're going to print two day shipping, and then we're going to break if case is three semi colon and they were going to print three day shipping and every break and then default.

  • So if it's not 12 or three, if it's anything else print, we do not ship to you.

  • Print, break print, please contact customer support.

  • And then this is where you actually do the break out of the switch.

  • Semi colon and the new clothes with this squiggly bracket.

  • And so this is how it works.

  • Remember, there's no there's no equal signs here.

  • There is no greater than less than six science here it is case and then the value that you're looking for.

  • So if its numeric, then it's just one, so you can put 1 10 20 or whatever here if it's a string than you would do.

  • The single quotation marks you know, if it's a name or something, you would put Bob in.

  • So if it is, if it's a string, you put the single quotation marks.

  • But if it's simply number, you just put the number there, and that's all there is to it.

  • If we go over now, we can see that we have region and we've set the value toe one.

  • So if we go over here Silicon del ACO switch And so since the value was one, it is one day shipping.

  • If we change this to to it file, it saved.

  • I blow this.

  • Now, if you refresh two day shipping again, same is true.

  • If we had three file, we have safe.

  • We upload and then now says three day shipping.

  • So as we change the value here, then it's different within the switch.

  • Now what if we say 33?

  • So 33 obviously is not one.

  • It's not, too.

  • It's not three.

  • So that would fall under the default.

  • You do save, upload and then this is we do not shipped to you Police con contact customer support.

  • So that's the basic idea of how a switch statement is written and kind of gives you an idea of why it's very useful in PHP.

  • So that's all there is to a switch statement in PHP.

  • So this is how you can check to see what the value of a variable is, and then based off of what the value is, different events can happen.

  • So again you can say one day shipping or two day shipping or you don't ship it all, or basically, depending on whatever the value of that variable is, you can have code that is run, so this is very good again.

  • If you have Web forms where people are plugging in certain information, it can go through and again, depending on what state they live in, or depending on different information.

  • If you have a known number of values, you can go and you can check on what the value of that variable is and whatever that is.

  • Then you can run the code on so that this makes it very simple, and especially if you've been using else if statements, this is an easier way to just be able to write, not just write the code, but also, as you can see, to read the code to know what's happening.

  • So that's what a statement is in PHP, and that's why it matters.

So let's talk about the switches statement in PHP.

字幕と単語

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

A2 初級

PHP - スイッチ ステートメント (PHP - switch Statements)

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