So now we're going to learn the match statement.
That's something similar to the switch statement though there are some differences.
Let's start by creating a match PHP file and the PHP tag.
So in this example I'd like to convert the HTTP status codes, which are always returned with every
response from the server to some meaningful text that would basically describe of to which group this
status belongs.
And for that we're going to use the match statement.
And already that's the first difference.
I can immediately assign the result of the match statement to a variable, because match returns a value.
So essentially it can be described as an expression.
So with the switch statement that wasn't the case.
You just used switch statement to execute some code.
And you would have to assign something to the message variable in every single case for it to work the
same way here, I can directly get the result from the match statement.
Let's add a semicolon here.
And how do you define cases?
Well, this syntax here is short.
Well it's shorter than in the switch statement.
So I can just list cases like that 200 300.
And I use this arrow symbol.
Again these are just two characters together.
And I return something.
I don't have to write the return keyword or anything.
Just whatever I type here is being returned.
So for those codes we assume it's a success.
Now obviously there are more HTTP codes.
We are just having a simple example here.
Then for 400 404 not found and 500 internal server error, we just return that.
That's an error.
And also we will have a default case that does an unknown status.
Finally let's just Strauss echoed the message.
And make sure we have a new line character.
So another difference with the match statement.
Comparing it to switch is that you don't need to use the break statement.
So this is by default working more like the if statement.
So we we are essentially trying to match the value of this expression to some specific values.
And then we return a specific value if there is a match.
So let's run it.
We do PHP match PHP.
That's an error because our status code was error.
If I'm gonna try that with the success, this also works.
And if I come up with something that doesn't make sense for HTTP, we get the unknown status.
Another thing you should keep in mind when using match is that it is using the strict comparison, which
means that not only will it compare a value, it will also compare the type.
So there is no type coercion in the match statement.
So essentially let me give you an example.
With the switch statement it was loosely compared, which means that in case of the switch statement
string 600 is the same as the number 600 because the type coercion is happening there.
But with the strict comparison, the text 600 is different then the number 600, because here the match
statement is going to be using the strict equality operator.
So as a reminder, also checking the type.
Now what also needs to be said about the match statement is that it was only introduced in PHP 8.0.
So you need to have at least this version of PHP.
It didn't exist previously.
It's quite new, but I really love this match statement and I think it was really necessary to add something
like this to the PHP language.