switch and Case match constructs in php

Switch..case construction

The construct switch..caseis an alternative to using the construct if..elseif..else. The switch statement takes some expression and compares it to a set of values:

switch (expression){
    case value1: actions; break;
    case value2: actions; break;
    //......................
    case valueN: actions; break;
}

The switch keyword is followed by the comparison expression in parentheses. The value of this expression is sequentially compared with the values ​​placed after the case statements . And if a match is found, then a specific case block will be executed.

A switch construct can contain an arbitrary number of case statements. A break statement is placed at the end of the case block to avoid executing other blocks.

The entire switch block is enclosed in curly brackets, however, the block of each individual case statement is NOT enclosed in curly brackets.

For example, take the following construction if..elseif..else:

$a = 3;
if($a==1) echo "addition";
elseif($a==2) echo "subtraction";
elseif($a==3) echo "multiply";
elseif($a==4) echo "divide";

Now let’s rewrite it using the construction switch..case:

$a = 3;
switch($a)
{
    case 1:
        echo "addition";
        break;
    case 2:
        echo "subtraction";
        break;
    case 3:
        echo "multiplication";
        break;
    case 4:
        echo "division";
        break;
}

That is, a variable is passed to the input of the switch construction $a , and its value is sequentially compared with the values ​​specified after the operators case. So, in this case, the variable $ais 3, so the execution will reach the block

case 3: 
    echo "multiplication";
    break;

and this block will be executed.

If we also want to handle the situation when no match is found, we can add a default block :

$a = 3;
switch($a)
{
    case 1:
        echo "addition";
        break;
    case 2:
        echo "subtraction";
        break;
    default:
        echo "default action";
        break;
}

The construct switch..case also supports an alternative syntax that replaces the opening curly brace with a colon and replaces the closing curly brace with the keyword endswitch :

    $a = 3;
switch($a):
    case 1:
        echo "addition";
        break;
    case 2:
        echo "subtraction";
        break;
    default:
        echo "default action";
        break;
endswitch

match

Since version 8.0 PHP has added support for another, similar construct, match. It allows you to optimize the design switch. The match construct also takes an expression and compares it against a set of values.

For example, let’s say we have the following code:

$a = 2;
switch($a)
{
    case 1:
        $operation = "addition";
        break;
    case 2:
        $operation = "subtraction";
        break;
    default:
        $operation = "default action";
        break;
}
echo $operation;

Let’s rewrite this example using match:

$a = 2;
$operation = match($a)
{
    1 => "addition",
    2 => "subtraction",
    default => "default action",
};
echo $operation;

So, matchin brackets, it also accepts some comparison expression (in this case, it is the variable $a). The match code block itself is also wrapped in curly braces, but a semicolon must be placed after the closing curly brace at the end. And instead of operators case, the values ​​\u200b\u200b with which the expression is compared are simply indicated.

But unlike , the match switch construct returns some result. Therefore, after each comparable value, the => operator is placed , followed by the returned result.

That is, in this case, the variable $a is equal to 2, so in the match construct, the block will execute

2 => "subtraction",

This block will set the return result to a string “subtraction”.

Since the match construct returns a result, we can assign this result to another variable:

$operation = match($a){
    //.................
}

$operation As a result, the string will be stored in the variable “subtraction”.

We can also rewrite the previous example as follows:

$a = 2;
match($a)
{
    1 => $operation = "addition",
    2 => $operation = "subtraction",
    default => $operation = "default action",
};
echo $operation;

Comparing values ​​and types

It is worth noting an important difference between the switch and match constructs: the switch only compares the value but does not take into account the type of the expression. Whereas match also takes into account the type of the expression being compared. Let’s look at the difference with an example. Let there be the following switch construction: easywptutorials.com

switch(8.0) {
  case "8.0":
    $result = "string";
    break;
  case 8.0:
    $result = "number";
    break;
}
echo $result; // line

The number 8.0 is passed to the switch construct as an expression, but from the point of view of the internal logic of the switch construct, this expression also corresponds to the string “8.0”. Therefore, in this case, the block will be executed

case "8.0":
    $result = "string";
    break;

Now let’s see what happens in a similar example with a match:

 
match(8.0) {
  "8.0" => $result = "string",
  8.0 => $result = "number"
};
echo $result; // number

The match construct will also take into account the type of the expression, and the type, in this case, is float, so the block will be executed:

8.0 => $result = "number"