Okay.
So let's talk about another control structure the switch statement.
Let me create the switch PHP file.
Add a PHP tag and let's see what is this about.
So switch statement is very similar to the if statement but there are some differences.
Let me start with an example.
And we're gonna see how it differs from an if statement.
So as an example here I'd like to use that t shirt sizes.
And we're just gonna evaluate the value of the size variable and output the text that will describe
this size based on the size variable value.
So let's start with the switch statement which begins with the switch keyword and the parentheses.
There goes the expression.
So the first difference with the if statement is that in the if statements, you add an expression that
is being evaluated to Boolean, and if this is true, then you run the code inside the if statement
body or any of the alternatives when you use the else if with switch, there is a different logic to
this.
You add an expression to the switch statement.
So here we're gonna evaluate the actual value of size, not whether it is true or false.
And then you just add cases like you add a case where the value is m and you echo that this is small
or medium size.
So corresponding if statement would be if m equals size then you'd like to echo this text.
So the text I've echoed here is small or medium size, which means I'd like to echo the same text if
the size is either medium or small.
So with the switch statement, I can handle both cases by adding another case statement and check if
the value of size is either s or m.
So the corresponding if statement is whether m equals size or whether s equals size.
Okay.
So now let's handle the large and XL sizes.
So for that I can also paste this code.
And there I add L and XL.
And I say large or extra large size.
Now if the size is unknown There is something similar to Els in if, which would be the default case
which is run in.
If none of the above cases match, so then I echo unknown size.
Now let me add a corresponding one if statement.
Now let me add a corresponding if statement.
So here I would have to.
Now let's add a corresponding if statement.
So I'm adding an else if and checking for large or extra large and echoing this text.
Then I also need an else alternative where we echo dot text.
So as we see there are some differences.
Maybe there is more lines of code with the switch statement, but other than that, it all boils down
to the preference of which one you would prefer.
So now let's go to some unique features of switch that really differs this statement from the if statement.
So it's time to run this.
I'm going to add a new line character to all those strings.
So it's clearly visible in the output.
And I think you might be a little bit surprised with the output of this.
So we are running both switch and if statements.
So let me run PHP switch PHP.
So this is the output.
We got small or medium size but also the large also the unknown size text.
And then again small or medium size.
So what happened here is this three lines come from the switch statement and the last one from the if
statement.
And that's a big difference with how the switch and if statements behave.
So first the if statement.
In this case you just check for some conditions and only then you run the code that is under this condition.
So it is either this or this or that.
You can't run a couple of those statements from different conditions because they are mutually exclusive,
unless your conditions are the same.
Now with the switch statement it works a little bit different.
So the way switch statement works is given the expression it tries to find a matching case.
So in our case that's M.
And this first case is already matching the value of size.
So this statement was run.
But what happened next is all the other statements were run as well.
This is a kind of a fallback nature of the switch statement.
Now, if you don't want this to happen, you would need to add a break statement after every single
case.
We're gonna do that in a second.
But first, let me change the size to L so that the first case won't match.
Now let's run the switch PHP script.
And now we see that we only got the large or extra size.
But then the also default case, the unknown size and the if statement stays the same.
It only echoes one text because it can only match one condition.
So if we would like the switch statement to behave like the if statement after every single one of those
cases, we need to add a break statement except the default one, because that's the last one.
Nothing else will run after it.
So the way it would work right now is if the switch statement will find a matching case for whatever
expression you evaluate, like let's say this one, the large size, it will run all the statements
that you have right here.
And then it will break this switch statement.
It will exit it so no other cases will be run.
So let's see that now we have the exactly the same behavior of the if and switch statements.
So it seems that with the switch statement, we just need to write a lot more code than with the if
statement.
And maybe that's the reason why the switch statement is less popular.
But actually this behavior of the switch statement is for a reason.
So maybe let's see another example.
When this behavior of switch statement will make a lot more sense.
Now to discuss this behavior of the switch statement, I would like to jump to Stack Overflow because
I found this interesting discussion here.
It's about C plus plus.
But essentially the switch statement is exactly the same in PHP like in C plus plus.
And people discuss the nature of switch statement and including the break and how it behaves without
it.
And this statement is interesting that not using break statement is generally considered a bad practice,
but sometimes it may be handy.
Well, I've also heard that not using break statement is sometimes considered a bad practice, but we
also have to think that if this would be a bad practice, why would this be part of the language?
Why would this be even allowed.
So I think it might be.
Maybe I'm gonna call it controversial that this is possible.
And that's the way just the switch statement works.
It has this fall back nature.
I think this here is a great example of why not using the break statement might be beneficial.
So here you've got the amount of bad login attempts when someone tried to log in and failed.
So you go from top to bottom.
If someone already tried three times you do something, you alert the admin, you block the account.
Doesn't matter what this does exactly, but then you don't break.
You always execute the next statements.
If there were only two bad attempts at signing in, logging in, and one attempt, and then you always
log this error and increase the number of bad attempts.
So it's something like do it all the time.
And in some special cases, like higher up cases, also do some additional things.
Okay.
So let's also try to implement this example.
Let's define the bad attempts variable.
Initialize it with two for now.
Then let me add a switch statement that evaluates this bad attempts.
And essentially you just need to put an expression here.
Doesn't have to be a variable.
And then let's add the cases.
So we start with three.
And then I'm just going to echo something here that you are blocked.
But then we also need to handle the other cases.
So we don't necessarily in this case need to add two and one because just a default case would be enough.
But let's add it.
So case two and case one.
So if the value of bad attempts is either 2 or 1 we log it.
That bad attempt detected.
And that's it.
So let's now see how this would behave.
So let's run the switch script.
Okay.
So we've got bad attempt detected because we've got two.
Now actually increasing this variable would not make sense because we just run this script once in the
real world we're going to get this value from some other external storage.
Now let me change that to three and see how this would behave.
So we are blocked.
But then we also kind of run some default code that detects the bad bunt attempt, you might want to
log it someplace or do something else with it.
But as I've said in this specific case, also default would make sense.
So let's run it again.
And if I change it to two back then this also works.
Now finally you should know that into this switch statement.
So the value that it gets evaluated you can put any expression doesn't have to be a variable.
This is also a fine expression.
Or you can have a function call if you prefer.
And this also applies to the case instructions.
Here you can also add an expression like for any reason you can check for that case that's different
from languages like C or Java for example.
But I think it's just useful to know now.
All in all.
To summarize, I think that you should just watch out with using the switch statement like this.
So using it as a kind of a fallback structure where you want some code to be run always and then in
some special cases also add some additional statements, because I just believe that this behavior of
the switch statement is just not widely understood.
And you also might hear that that's a bad practice not to use the break statements.
So that's why I called this a little bit controversial.
I think it's fine if the language allows that.
But just be warned.
That's just something that not everyone understands perfectly, and sometimes it just might be a good
practice to avoid writing code.
That is quite possible.
Other people won't understand.