payment by upi: sinhamit@icici or payment by bank account name: amit kumar sinha, account number: 2646728782 IFSC code: KKBK0005660 SWIFT: KKBKINBB

Please support if you like my work by payment through upi: sinhamit@icici or payment by bank

account name: Amit Kumar Sinha,
account number: 2646728782
IFSC code: KKBK0005660
SWIFT: KKBKINBB


match & switch   in Category: PHP   by amit

🕙 Posted on 2023-06-09 at 11:52:51     Read in Hindi ...


match expression & More on switch

    match expression has been introduced in PHP 8.0.0. Unlike switch, match will evaluate to a value much like ternary expressions. Also, unlike switch, the match comparison is an identity check (===) rather than a weak/loose equality check (==). match expressions are described at the bottom of this page.

    You have seen similarity between the switch and if-elseif-else blocks, in previous page. The following example is to explain you that code on previous page to find grade from if-elseif-else blocks can be written as follows. In the following example, when statements after multiple case options, that is, after : (colon symbol) are exactly same, you don't have to write same duplication of statements for each of those case options.

    When the data (or value) to be checked, is compared with a case literal, all statements below it will be executed, till the PHP interpreter finds any break statement, or closing curly brace } of switch block. It is possible to use a ; (semicolon) instead of a : (colon) after a case as like in following example.

<?php
  $my_score = 75;
  switch( $my_score ) {
    case 100:
    case 99:
    case 98:
    case 97:
    case 96:
    case 95:
      echo 'A+';   break;
    case 94:
    case 93:
    case 92:
    case 91:
    case 90:
      echo 'A';   break;
    case 89:
    case 88:
    case 87:
    case 86:
    case 85:
      echo 'B+';   break;
    case 84:
    case 83:
    case 82:
    case 81:
    case 80:
      echo 'B';   break;
    case 79:
    case 78:
    case 77:
    case 76:
    case 75:
      echo 'C+';   break;
    case 74:
    case 73:
    case 72:
    case 71:
    case 70:
      echo 'C';   break;
    case 69:
    case 68:
    case 67:
    case 66:
    case 65:
      echo 'D+';   break;
    case 64:
    case 63:
    case 62:
    case 61:
    case 60:
      echo 'D';   break;
    case 59:
    case 58:
    case 57:
    case 56:
    case 55:
      echo 'E+';   break;
    case 54:
    case 53:
    case 52:
    case 51:
    case 50:
      echo 'E';   break;
    default:
      echo 'F';
  }
?>

    It is obvious to easily write if-elseif-else series of blocks, for above example, as described in previous page. Every option after the case keyword will be evaluated as expression, if you use them instead of literals. The expression after the case keyword must be a conditional expression. When the output of such expression is true, then all statements after that case will be executed. The above example can be written as follows −

<?php
  $my_score = 75;
  switch( true ) {
    case $my_score - 50 >= 45:
      echo 'A+';   break;
    case $my_score - 50 >= 40:
      echo 'A';   break;
    case $my_score - 50 >= 35:
      echo 'B+';   break;
    case $my_score - 50 >= 30:
      echo 'B';   break;
    case $my_score - 50 >= 25:
      echo 'C+';   break;
    case $my_score - 50 >= 20:
      echo 'C';   break;
    case $my_score - 50 >= 15:
      echo 'D+';   break;
    case $my_score - 50 >= 10:
      echo 'D';   break;
    case $my_score - 50 >= 5:
      echo 'E+';   break;
    case $my_score - 50 >= 0:
      echo 'E';   break;
    default:
      echo 'F';
  }
?>

    In the above example, if you write literal value 1 after a case, no matter what value is assigned to $my_score, when that case is encountered by the PHP interpreter, the statements after it, will be output (loose comparison true is evaluated as 1).

    However, the literal value after the case keyword can be of string data-type. In this case, the switch block is very useful, to compare the string data with a case literal. The following example explains the usefulness of switch block, in a CMS (or complex website structure).

<?php
  $cms_user = 'admin';
  switch( $cms_user ) {
    case 'admin':
      echo 'You have permission to everything.';   break;
    case 'author':
      echo 'You have permission to write content only.';   break;
    case 'subscriber':
      echo 'You have permission to read all tutorials.';   break;
    case 'manager':
      echo 'You have permission to manage user data only.';   break;
    case 'public':
      echo 'You have permission to free tutorials only.';   break;
    default:
      echo 'You have not permission to anything. Please subscribe.';
  }
?>


match expressions

    A match expression must be exhaustive. It means that when no options inside a match expression are strictly identical to data (to be compared) passed in () parentheses, an ERROR will be shown. A match expression must be terminated by a ; (semicolon) at the end, after } (closing curly brace). A match expression returns a value, and it can be stored to a variable/constant name.

    A switch statement is a block of codes, and inside the {} (curly braces), you have to terminate all statements after case with ; (semicolon). But, a match is actually an experssion, and all statements inside the {} (curly braces) are separated by , (comma). The syntax of match(){} expression is similar to that of an associative array.

<?php
  $my_grade = 1; // a simple match expression is shown below
  $my_result = match( $my_grade ) {
    0 => 'ZERO',     1 => 'ONE',
    2 => 'TWO',       3 => 'THREE',
    4 => 'FOUR',     5 => 'FIVE',
    default => 'other'
  };
  echo $my_result; // outputs ONE
?>

    As you can see in the above example that in a match expression, there is no requirement of break, or case keywords. A match expression only checks one condition and throws others. In a switch block, when default keyword is not provided, and no case is matched with data provided in () parentheses, there will be no ERROR. But the match expression will throw UnhandledMatchError when no arm (option) inside {} curly braces is matched with the data provided in the () parentheses.

<?php
  $my_score = 75; // match expression similar to switch block at the top is shown below
  $my_result = match( $my_score ) {
    100, 99, 98, 97, 96, 95 => 'A+',
    94, 93, 92, 91, 90         => 'A',
    89, 88, 87, 86, 85         => 'B+',
    84, 83, 82, 81, 80         => 'B',
    79, 78, 77, 76, 75         => 'C+',
    74, 73, 72, 71, 70         => 'C',
    69, 68, 67, 66, 65         => 'D+',
    64, 63, 62, 61, 60         => 'D',
    59, 58, 57, 56, 55         => 'E+',
    54, 53, 52, 51, 50         => 'E',
    $my_score < 50         => 'F',
    default => 'other'
  };
  echo $my_result; // outputs C+
?>

<?php
  $my_score = 75; // the above match expression can also be written as follows
  $my_result = match( true ) {
    $my_score >= 95 => 'A+',
    $my_score >= 90 => 'A',
    $my_score >= 85 => 'B+',
    $my_score >= 80 => 'B',
    $my_score >= 75 => 'C+',
    $my_score >= 70 => 'C',
    $my_score >= 65 => 'D+',
    $my_score >= 60 => 'D',
    $my_score >= 55 => 'E+',
    $my_score >= 50 => 'E',
    $my_score < 50 => 'F',
    default => 'other'
  };
  echo $my_result; // outputs C+
?>

<?php
  $cms_user = 'admin';
  $my_result = match( $cms_user ) {
    'admin' => 'You have permission to everything.',
    'author' => 'You have permission to write content only.',
    'subscriber' => 'You have permission to read all tutorials.',
    'manager' => 'You have permission to manage user data only.',
    'public' => 'You have permission to free tutorials only.',
    default => 'You have not permission to anything. Please subscribe.'
  };
  echo $my_result; // outputs You have permission to everything.
?>

    In a match expression, the statement after the => (arrow) operator can be replaced with some (user-defined) custom function. In next category, that is, PHP level 2, you will learn about how to create custom functions, and much more...


Leave a Comment: