WEBVTT

00:00.920 --> 00:04.190
So now we're going to learn the match statement.

00:04.190 --> 00:10.310
That's something similar to the switch statement though there are some differences.

00:10.340 --> 00:15.860
Let's start by creating a match PHP file and the PHP tag.

00:16.550 --> 00:24.320
So in this example I'd like to convert the HTTP status codes, which are always returned with every

00:24.320 --> 00:33.020
response from the server to some meaningful text that would basically describe of to which group this

00:33.020 --> 00:34.670
status belongs.

00:34.670 --> 00:37.490
And for that we're going to use the match statement.

00:37.490 --> 00:41.120
And already that's the first difference.

00:41.120 --> 00:49.970
I can immediately assign the result of the match statement to a variable, because match returns a value.

00:49.970 --> 00:54.140
So essentially it can be described as an expression.

00:54.140 --> 00:58.340
So with the switch statement that wasn't the case.

00:58.340 --> 01:01.880
You just used switch statement to execute some code.

01:01.880 --> 01:09.180
And you would have to assign something to the message variable in every single case for it to work the

01:09.180 --> 01:14.790
same way here, I can directly get the result from the match statement.

01:15.150 --> 01:17.100
Let's add a semicolon here.

01:17.100 --> 01:19.440
And how do you define cases?

01:19.440 --> 01:22.140
Well, this syntax here is short.

01:22.170 --> 01:25.230
Well it's shorter than in the switch statement.

01:25.740 --> 01:30.030
So I can just list cases like that 200 300.

01:30.030 --> 01:32.520
And I use this arrow symbol.

01:32.550 --> 01:35.670
Again these are just two characters together.

01:36.600 --> 01:38.340
And I return something.

01:38.340 --> 01:41.820
I don't have to write the return keyword or anything.

01:41.820 --> 01:45.270
Just whatever I type here is being returned.

01:45.270 --> 01:49.680
So for those codes we assume it's a success.

01:49.680 --> 01:51.990
Now obviously there are more HTTP codes.

01:51.990 --> 01:54.330
We are just having a simple example here.

01:54.510 --> 02:03.390
Then for 400 404 not found and 500 internal server error, we just return that.

02:03.390 --> 02:04.560
That's an error.

02:04.560 --> 02:12.900
And also we will have a default case that does an unknown status.

02:14.760 --> 02:17.800
Finally let's just Strauss echoed the message.

02:17.800 --> 02:21.190
And make sure we have a new line character.

02:22.570 --> 02:25.060
So another difference with the match statement.

02:25.060 --> 02:30.550
Comparing it to switch is that you don't need to use the break statement.

02:30.550 --> 02:35.350
So this is by default working more like the if statement.

02:35.350 --> 02:43.120
So we we are essentially trying to match the value of this expression to some specific values.

02:43.120 --> 02:47.950
And then we return a specific value if there is a match.

02:49.420 --> 02:50.560
So let's run it.

02:50.590 --> 02:53.260
We do PHP match PHP.

02:53.740 --> 02:57.580
That's an error because our status code was error.

02:57.580 --> 03:01.420
If I'm gonna try that with the success, this also works.

03:01.420 --> 03:09.850
And if I come up with something that doesn't make sense for HTTP, we get the unknown status.

03:11.320 --> 03:17.890
Another thing you should keep in mind when using match is that it is using the strict comparison, which

03:17.890 --> 03:23.590
means that not only will it compare a value, it will also compare the type.

03:23.590 --> 03:27.410
So there is no type coercion in the match statement.

03:27.410 --> 03:29.870
So essentially let me give you an example.

03:29.870 --> 03:36.500
With the switch statement it was loosely compared, which means that in case of the switch statement

03:36.530 --> 03:44.900
string 600 is the same as the number 600 because the type coercion is happening there.

03:45.230 --> 03:57.890
But with the strict comparison, the text 600 is different then the number 600, because here the match

03:57.890 --> 04:02.450
statement is going to be using the strict equality operator.

04:02.450 --> 04:05.480
So as a reminder, also checking the type.

04:07.760 --> 04:17.180
Now what also needs to be said about the match statement is that it was only introduced in PHP 8.0.

04:17.270 --> 04:21.380
So you need to have at least this version of PHP.

04:21.410 --> 04:23.240
It didn't exist previously.

04:23.240 --> 04:30.740
It's quite new, but I really love this match statement and I think it was really necessary to add something

04:30.740 --> 04:33.290
like this to the PHP language.
