WEBVTT

00:00.410 --> 00:00.710
Okay.

00:00.710 --> 00:05.210
So let's talk about another control structure the switch statement.

00:05.390 --> 00:09.080
Let me create the switch PHP file.

00:09.230 --> 00:14.360
Add a PHP tag and let's see what is this about.

00:14.390 --> 00:22.190
So switch statement is very similar to the if statement but there are some differences.

00:22.220 --> 00:25.130
Let me start with an example.

00:25.130 --> 00:29.660
And we're gonna see how it differs from an if statement.

00:30.380 --> 00:35.180
So as an example here I'd like to use that t shirt sizes.

00:35.180 --> 00:44.210
And we're just gonna evaluate the value of the size variable and output the text that will describe

00:44.210 --> 00:48.140
this size based on the size variable value.

00:48.170 --> 00:55.760
So let's start with the switch statement which begins with the switch keyword and the parentheses.

00:55.760 --> 00:57.500
There goes the expression.

00:57.500 --> 01:05.180
So the first difference with the if statement is that in the if statements, you add an expression that

01:05.180 --> 01:12.920
is being evaluated to Boolean, and if this is true, then you run the code inside the if statement

01:12.920 --> 01:20.150
body or any of the alternatives when you use the else if with switch, there is a different logic to

01:20.180 --> 01:20.840
this.

01:21.380 --> 01:23.960
You add an expression to the switch statement.

01:23.960 --> 01:31.730
So here we're gonna evaluate the actual value of size, not whether it is true or false.

01:31.730 --> 01:43.430
And then you just add cases like you add a case where the value is m and you echo that this is small

01:43.430 --> 01:45.710
or medium size.

01:46.820 --> 01:59.180
So corresponding if statement would be if m equals size then you'd like to echo this text.

02:00.050 --> 02:08.180
So the text I've echoed here is small or medium size, which means I'd like to echo the same text if

02:08.180 --> 02:10.970
the size is either medium or small.

02:11.000 --> 02:18.260
So with the switch statement, I can handle both cases by adding another case statement and check if

02:18.260 --> 02:22.280
the value of size is either s or m.

02:23.540 --> 02:34.670
So the corresponding if statement is whether m equals size or whether s equals size.

02:37.010 --> 02:37.280
Okay.

02:37.280 --> 02:40.880
So now let's handle the large and XL sizes.

02:40.880 --> 02:43.790
So for that I can also paste this code.

02:43.790 --> 02:47.270
And there I add L and XL.

02:47.270 --> 02:53.420
And I say large or extra large size.

02:53.990 --> 03:02.780
Now if the size is unknown There is something similar to Els in if, which would be the default case

03:02.780 --> 03:03.890
which is run in.

03:03.920 --> 03:10.910
If none of the above cases match, so then I echo unknown size.

03:10.940 --> 03:15.080
Now let me add a corresponding one if statement.

03:16.130 --> 03:18.920
Now let me add a corresponding if statement.

03:18.920 --> 03:21.470
So here I would have to.

03:26.390 --> 03:29.300
Now let's add a corresponding if statement.

03:29.570 --> 03:40.130
So I'm adding an else if and checking for large or extra large and echoing this text.

03:41.240 --> 03:48.680
Then I also need an else alternative where we echo dot text.

03:48.770 --> 03:52.610
So as we see there are some differences.

03:52.610 --> 04:00.620
Maybe there is more lines of code with the switch statement, but other than that, it all boils down

04:00.620 --> 04:04.190
to the preference of which one you would prefer.

04:04.280 --> 04:13.340
So now let's go to some unique features of switch that really differs this statement from the if statement.

04:14.360 --> 04:15.950
So it's time to run this.

04:15.980 --> 04:19.340
I'm going to add a new line character to all those strings.

04:19.340 --> 04:22.820
So it's clearly visible in the output.

04:22.820 --> 04:28.490
And I think you might be a little bit surprised with the output of this.

04:28.490 --> 04:32.000
So we are running both switch and if statements.

04:32.090 --> 04:35.360
So let me run PHP switch PHP.

04:35.870 --> 04:37.640
So this is the output.

04:37.640 --> 04:43.610
We got small or medium size but also the large also the unknown size text.

04:43.610 --> 04:46.340
And then again small or medium size.

04:46.340 --> 04:53.570
So what happened here is this three lines come from the switch statement and the last one from the if

04:53.570 --> 04:54.290
statement.

04:54.290 --> 04:59.990
And that's a big difference with how the switch and if statements behave.

05:01.640 --> 05:03.620
So first the if statement.

05:03.650 --> 05:11.090
In this case you just check for some conditions and only then you run the code that is under this condition.

05:11.090 --> 05:15.710
So it is either this or this or that.

05:15.740 --> 05:22.880
You can't run a couple of those statements from different conditions because they are mutually exclusive,

05:22.910 --> 05:25.340
unless your conditions are the same.

05:25.370 --> 05:29.750
Now with the switch statement it works a little bit different.

05:30.170 --> 05:38.000
So the way switch statement works is given the expression it tries to find a matching case.

05:38.000 --> 05:40.220
So in our case that's M.

05:40.220 --> 05:45.710
And this first case is already matching the value of size.

05:45.710 --> 05:47.810
So this statement was run.

05:47.810 --> 05:53.030
But what happened next is all the other statements were run as well.

05:53.030 --> 05:57.890
This is a kind of a fallback nature of the switch statement.

05:57.920 --> 06:04.430
Now, if you don't want this to happen, you would need to add a break statement after every single

06:04.430 --> 06:05.030
case.

06:05.030 --> 06:06.650
We're gonna do that in a second.

06:06.650 --> 06:13.220
But first, let me change the size to L so that the first case won't match.

06:14.750 --> 06:17.720
Now let's run the switch PHP script.

06:17.720 --> 06:22.010
And now we see that we only got the large or extra size.

06:22.010 --> 06:28.160
But then the also default case, the unknown size and the if statement stays the same.

06:28.160 --> 06:35.150
It only echoes one text because it can only match one condition.

06:37.430 --> 06:43.640
So if we would like the switch statement to behave like the if statement after every single one of those

06:43.640 --> 06:50.960
cases, we need to add a break statement except the default one, because that's the last one.

06:50.960 --> 06:53.180
Nothing else will run after it.

06:53.180 --> 07:01.790
So the way it would work right now is if the switch statement will find a matching case for whatever

07:01.790 --> 07:09.020
expression you evaluate, like let's say this one, the large size, it will run all the statements

07:09.020 --> 07:10.130
that you have right here.

07:10.130 --> 07:13.910
And then it will break this switch statement.

07:13.910 --> 07:17.750
It will exit it so no other cases will be run.

07:18.890 --> 07:26.480
So let's see that now we have the exactly the same behavior of the if and switch statements.

07:26.660 --> 07:33.470
So it seems that with the switch statement, we just need to write a lot more code than with the if

07:33.470 --> 07:34.040
statement.

07:34.040 --> 07:39.230
And maybe that's the reason why the switch statement is less popular.

07:39.230 --> 07:45.470
But actually this behavior of the switch statement is for a reason.

07:45.470 --> 07:47.780
So maybe let's see another example.

07:47.780 --> 07:53.220
When this behavior of switch statement will make a lot more sense.

07:54.450 --> 08:02.220
Now to discuss this behavior of the switch statement, I would like to jump to Stack Overflow because

08:02.220 --> 08:04.980
I found this interesting discussion here.

08:05.250 --> 08:06.660
It's about C plus plus.

08:06.660 --> 08:13.230
But essentially the switch statement is exactly the same in PHP like in C plus plus.

08:13.230 --> 08:20.880
And people discuss the nature of switch statement and including the break and how it behaves without

08:20.880 --> 08:21.420
it.

08:21.570 --> 08:30.210
And this statement is interesting that not using break statement is generally considered a bad practice,

08:30.210 --> 08:33.510
but sometimes it may be handy.

08:33.540 --> 08:42.450
Well, I've also heard that not using break statement is sometimes considered a bad practice, but we

08:42.450 --> 08:48.870
also have to think that if this would be a bad practice, why would this be part of the language?

08:48.870 --> 08:51.090
Why would this be even allowed.

08:51.090 --> 08:54.690
So I think it might be.

08:54.690 --> 08:59.400
Maybe I'm gonna call it controversial that this is possible.

08:59.400 --> 09:03.000
And that's the way just the switch statement works.

09:03.000 --> 09:05.310
It has this fall back nature.

09:05.310 --> 09:13.170
I think this here is a great example of why not using the break statement might be beneficial.

09:13.170 --> 09:21.390
So here you've got the amount of bad login attempts when someone tried to log in and failed.

09:22.830 --> 09:25.410
So you go from top to bottom.

09:25.410 --> 09:32.850
If someone already tried three times you do something, you alert the admin, you block the account.

09:32.880 --> 09:37.200
Doesn't matter what this does exactly, but then you don't break.

09:37.230 --> 09:40.290
You always execute the next statements.

09:40.290 --> 09:48.090
If there were only two bad attempts at signing in, logging in, and one attempt, and then you always

09:48.090 --> 09:52.530
log this error and increase the number of bad attempts.

09:52.530 --> 09:55.920
So it's something like do it all the time.

09:55.980 --> 10:03.900
And in some special cases, like higher up cases, also do some additional things.

10:07.050 --> 10:07.350
Okay.

10:07.350 --> 10:09.990
So let's also try to implement this example.

10:09.990 --> 10:13.200
Let's define the bad attempts variable.

10:13.200 --> 10:15.120
Initialize it with two for now.

10:15.240 --> 10:22.050
Then let me add a switch statement that evaluates this bad attempts.

10:22.050 --> 10:24.900
And essentially you just need to put an expression here.

10:24.930 --> 10:26.700
Doesn't have to be a variable.

10:26.700 --> 10:28.830
And then let's add the cases.

10:28.830 --> 10:30.690
So we start with three.

10:30.690 --> 10:37.950
And then I'm just going to echo something here that you are blocked.

10:39.690 --> 10:42.660
But then we also need to handle the other cases.

10:42.660 --> 10:50.820
So we don't necessarily in this case need to add two and one because just a default case would be enough.

10:51.780 --> 10:52.830
But let's add it.

10:52.830 --> 10:56.040
So case two and case one.

10:56.040 --> 11:02.490
So if the value of bad attempts is either 2 or 1 we log it.

11:02.520 --> 11:06.780
That bad attempt detected.

11:10.380 --> 11:11.190
And that's it.

11:11.220 --> 11:14.610
So let's now see how this would behave.

11:15.600 --> 11:18.570
So let's run the switch script.

11:18.840 --> 11:19.140
Okay.

11:19.170 --> 11:22.980
So we've got bad attempt detected because we've got two.

11:23.010 --> 11:29.880
Now actually increasing this variable would not make sense because we just run this script once in the

11:29.880 --> 11:34.650
real world we're going to get this value from some other external storage.

11:34.680 --> 11:38.580
Now let me change that to three and see how this would behave.

11:38.820 --> 11:40.920
So we are blocked.

11:40.920 --> 11:48.960
But then we also kind of run some default code that detects the bad bunt attempt, you might want to

11:48.990 --> 11:52.110
log it someplace or do something else with it.

11:52.110 --> 11:57.570
But as I've said in this specific case, also default would make sense.

11:59.040 --> 12:00.570
So let's run it again.

12:02.610 --> 12:07.050
And if I change it to two back then this also works.

12:09.300 --> 12:12.600
Now finally you should know that into this switch statement.

12:12.600 --> 12:18.840
So the value that it gets evaluated you can put any expression doesn't have to be a variable.

12:18.870 --> 12:21.090
This is also a fine expression.

12:21.120 --> 12:24.240
Or you can have a function call if you prefer.

12:24.390 --> 12:27.720
And this also applies to the case instructions.

12:27.720 --> 12:35.220
Here you can also add an expression like for any reason you can check for that case that's different

12:35.220 --> 12:38.340
from languages like C or Java for example.

12:38.910 --> 12:42.180
But I think it's just useful to know now.

12:42.180 --> 12:43.050
All in all.

12:43.230 --> 12:50.040
To summarize, I think that you should just watch out with using the switch statement like this.

12:50.040 --> 12:59.250
So using it as a kind of a fallback structure where you want some code to be run always and then in

12:59.250 --> 13:08.670
some special cases also add some additional statements, because I just believe that this behavior of

13:08.670 --> 13:12.480
the switch statement is just not widely understood.

13:12.480 --> 13:18.930
And you also might hear that that's a bad practice not to use the break statements.

13:18.930 --> 13:22.440
So that's why I called this a little bit controversial.

13:22.440 --> 13:25.500
I think it's fine if the language allows that.

13:25.500 --> 13:27.780
But just be warned.

13:27.810 --> 13:36.660
That's just something that not everyone understands perfectly, and sometimes it just might be a good

13:36.660 --> 13:39.360
practice to avoid writing code.

13:39.360 --> 13:42.630
That is quite possible.

13:42.660 --> 13:45.510
Other people won't understand.
