Hello and welcome to this PHP tutorial on the SWITCH statement.
I'm going to show you a new exercise on this because its an important feature of PHP
Let's create the syntax quickly
The SWITCH statement is a substitute for the IF statement. Its a lot more neater and formattable choice although the input is an expression.
So, now let's input the value of something and then let's save the value equal to this
Then we can execute the code if it equals or matches this value
It's not a comparing technique. So, for comparing an IF statement for matching values and outputs that depend on the input, we're going to say SWITCH
Let's start
SWITCH is the basic code for it
Let's put an expression in here, for example, I will say Alex here
Let's create a mini program and I will explain it as we go
Just like the IFstatement we will put curly brackets here
Now let's look at the way to call each sort of check
We want to check the value here
Now we will put this in quotation marks
You can't even number obviously
So what we type is - case - the value of the case that we want to match. For example- Alex
Then we type a colon or a semi colon
And then the condition if the case has matched with the SWITCH expression you have picked
So, I will type - echo you have blue eyes
To end our case comparison we're going to use break and a semi colon
Remember that we've used the semi colon here but not here
Now the second case. Lets see how to do it
I'll type Billy and echo you have brown eyes
Okay, and then break and semi-colon
This is like an integrated IF. That is I could say - IF your name is Alex then echo you have blue eyes or ELSE IF your name is Billy you have brown eyes
Probably for some people it's easy to do it this way. It's a lot more readable but it's a matter of choice
Okay we got no more cases, I'm just going to use Alex and Billy for this example
Here I will say default which will echo out - I don't know what color your eyes are
Okay, we don't need a break after this because there are no more cases
Obviously, there's no break after it cause there are no more options to chose from
Okay, so, we've got our SWITCH here. Let's give it a go
Now I'm going to replace this ALEX here with a variable, to build our program
So I'll type name equals, and I will let you decide that
Then I'll say name, here
So you see this is how we incorporate a variable here
You should know how to do that by now
So, let's start and see how this will work
You'll say switch, you'll take this expression, which is equal to Alex
Basically, this is the case which equals to Alex and it'll echo this. The break is to end it
If the name is say, Rahul, the default will echo - I don't know what colour your eyes are
Okay, so let's try running this
Just to revise
We can see that Alex matches to Alex matches to the output
What you can do is you can enter as many lines of code here as you like. This break determines where the case ends
An IF statement needs curly brackets to end a block
However, here break determines the end of the block. These are called blocks, by the way.
So, let's change this to Billy and let's see what happens
You have brown eyes - exactly what we determined here
Okay, now I'll change this to Kyle.....and refresh, I don't know what colour your eyes are, because there is no block that states Kyle's eye colour in our program feature
So, that's basically the SWITCH statement
Try it out. Some people don't like using it, some prefer using it
It's probably much faster than the IF statement. It's easier to control. It looks a lot better. So really its up to your personal choice
Thanks for watching. This is Arvind for the Spoken Tutorial Project, signing off. Goodbye.