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


switch statements   in Category: PHP   by amit

🕙 Posted on 2023-06-07 at 06:12:52     Read in Hindi ...


switch, break, continue

    Many programmers compare and think that switch(){} block is equivalent to if(){} elseif(){} else{} blocks. It may be correct in some case, but in my opinion, switch is used where you have multiple options for a variable/constant name. Whereas if(){} elseif(){} else{} can check many conditional expressions.

    The switch statement is similar to a series of IF statements on the same expression. In many occasions, you may want to compare the same variable (or expression) with many different values, and execute a different piece of code depending on which value it equals to. This is exactly what the switch statement is for. Let't examine following examples:

<?php
  $my_score = 75; // data (or value) to be checked.
  if( $my_score >= 90 ) {
    echo 'A';
  } elseif( $my_score >= 80 ) {
    echo 'B';
  } elseif( $my_score >= 70 ) {
    echo 'C';
  } elseif( $my_score >= 60 ) {
    echo 'D';
  } elseif( $my_score >= 50 ) {
    echo 'E';
  } else {
    echo 'F';
  }
?>

    The above if(){} elseif(){} else{} blocks can be more complex, in some practical cases as follows. In that situation, switch(){} is slightly faster than if(){} elseif(){} else{} blocks. It will also be proved when we will learn about user-defined custom functions.

<?php
  $my_score = 75;
  if( $my_score >= 90 ) {
    echo 'A';
    if ( $my_score >= 95 ) echo '+';
  } elseif( $my_score >= 80 ) {
    echo 'B';
    if ( $my_score >= 85 ) echo '+';
  } elseif( $my_score >= 70 ) {
    echo 'C';
    if ( $my_score >= 75 ) echo '+';
  } elseif( $my_score >= 60 ) {
    echo 'D';
    if ( $my_score >= 65 ) echo '+';
  } elseif( $my_score >= 50 ) {
    echo 'E';
    if ( $my_score >= 55 ) echo '+';
  } else {
    echo 'F';
  }
?>

switch(){} checks loose comparison.

    Let't see some simple switch(){} blocks in following examples.

<?php
  $my_grade = 1;
  switch( $my_grade ) {
    case 0:
        echo 'ZERO';
    case 1:
        echo 'ONE';
    case 2:
        echo 'TWO';
    case 3:
        echo 'THREE';
    case 4:
        echo 'FOUR';
    case 5:
        echo 'FIVE';
    default:
        echo 'other';
  }
?>

    As you can see in the above example, a switch block consists of at least switch, case, and default (reserved keywords). The last default is optional, and you can leave it. However, in the above example, all statements below a matched (i.e., compared) case (when the output of that comparison is true), will be executed. The output of above example is as follows:

ONETWOTHREEFOURFIVEother

    The above example is described herein to explain you that you also need break (reserved keyword) after every case option. The break keyword terminates the current block, and returns the output of all statements and expressions above the break keyword. Let't add break keyword in each case block in above example −

<?php
  $my_grade = 1;
  switch( $my_grade ) {
    case 0:   echo 'ZERO';   break;
    case 1:   echo 'ONE';   break;
    case 2:   echo 'TWO';   break;
    case 3:   echo 'THREE';   break;
    case 4:   echo 'FOUR';   break;
    case 5:   echo 'FIVE';   break;
    default:   echo 'other';
  }
?>

    The above example outputs ONE only, and the break keyword exits immediately out of the {} curly braces (that is, switch block). The above switch block is similar to following if-elseif-else block. While the conditional expression inside the switch() parentheses is evaluated only once, the conditional expression passed inside every parentheses of if() elseif() elseif() else block will be evaluated as many times as it is passed.

<?php
  $my_grade = 1;
  if( $my_grade == 0 ) { echo 'ZERO'; }
  elseif( $my_grade == 1 ) { echo 'ONE'; }
  elseif( $my_grade == 2 ) { echo 'TWO'; }
  elseif( $my_grade == 3 ) { echo 'THREE'; }
  elseif( $my_grade == 4 ) { echo 'FOUR'; }
  elseif( $my_grade == 5 ) { echo 'FIVE'; }
  else { echo 'other'; }
?>


    Though it is mentioned in the documentation that unlike some other languages, the continue statement applies to switch and acts similar to break, however, you should not use continue (reserved keyword) to skip the current switch block.

    In PHP the switch statement is considered a looping structure for the purposes of continue. continue behaves like break (when no arguments are passed) but will raise a warning as this is likely to be a mistake. If a switch is inside a loop, continue 2 will continue (skip) with the next iteration of the outer loop.

<?php
  $my_grade = 2;
  switch( $my_grade ) {
    case 0:   echo 'ZERO';   continue;
    case 1:   echo 'ONE';   continue;
    case 2:   echo 'TWO';   continue;
    case 3:   echo 'THREE';   continue;
    case 4:   echo 'FOUR';   continue;
    case 5:   echo 'FIVE';   continue;
    default:   echo 'other';
  }
?>

    The above example will output following WARNING. At the end the output is TWO when $my_grade = 2. The break 2 and continue 2 are used to break and skip the parent {} curly braces (block). These will be explained in next page.


Warning: "continue" targeting switch is equivalent to "break" in C:\xampp\htdocs\php_project\phptest.php on line 4

Warning: "continue" targeting switch is equivalent to "break" in C:\xampp\htdocs\php_project\phptest.php on line 5

Warning: "continue" targeting switch is equivalent to "break" in C:\xampp\htdocs\php_project\phptest.php on line 6

Warning: "continue" targeting switch is equivalent to "break" in C:\xampp\htdocs\php_project\phptest.php on line 7

Warning: "continue" targeting switch is equivalent to "break" in C:\xampp\htdocs\php_project\phptest.php on line 8

Warning: "continue" targeting switch is equivalent to "break" in C:\xampp\htdocs\php_project\phptest.php on line 9
TWO

    You will learn more about switch break continue etc. in next page.


Leave a Comment: