switch statement without break

后端 未结 6 1499
粉色の甜心
粉色の甜心 2021-01-17 21:35

How come a case option in a switch statement that does not contain a break automatically forwards to a next case without check?

try {
    switch($param) {
          


        
相关标签:
6条回答
  • 2021-01-17 22:17

    In PHP 8 we have match, similar with switch expression but is significantly shorter:

    • it doesn't require a break statement
    • it can combine different arms into one using a comma
    • it returns a value, so you only have to assign value once

    An example:

    $message = match ($statusCode) {
        200, 300 => null,
        400 => 'not found',
        500 => 'server error',
        default => 'unknown status code',
    };
    

    Here's its switch equivalent:

    switch ($statusCode) {
        case 200:
        case 300:
            $message = null;
            break;
        case 400:
            $message = 'not found';
            break;
        case 500:
            $message = 'server error';
            break;
        default:
            $message = 'unknown status code';
            break;
    }
    

    reference : https://stitcher.io/blog/php-8-match-or-switch

    0 讨论(0)
  • 2021-01-17 22:20

    Perhaps this will enlighten you:

    Jump Table Switch Case question

    0 讨论(0)
  • 2021-01-17 22:30

    I don't really see what you want.

    1. If you want to run the default stuff in all cases, just put it after the switch.
    2. If you want to run the default stuff only in the "created" case and in the default case, swap the position of the "created" and "Creator" sections and put a break after the first.
    3. If you want that code to only run if Creator or created matches, then get rid of the switch statement and use an if/else OR use a flag and a following if statement.

    All the tools are there.

    0 讨论(0)
  • 2021-01-17 22:31

    Fallthrough was an intentional design feature for allowing code like:

    switch ($command) {
      case "exit":
      case "quit":
        quit();
        break;
      case "reset":
        stop();
      case "start":
        start();
        break;
    }
    

    It's designed so that execution runs down from case to case.

    default is a case like any other, except that jumping there happens if no other case was triggered. It is not by any means a "do this after running the actual selected case" instruction. In your example, you could consider:

      switch($param) {
        case "created":
            if(!($value instanceof \DateTime))
                throw new \Exception("\DateTime expected, ".gettype($value)." given for self::$param");
            break;
        case "Creator":
            if(!($value instanceof \Base\User)) {
                throw new \Exception(get_class($value)." given. \Base\User expected for self::\$Creator");                  
            }
            break;
    }
    
    $this->$param = $value;
    

    The rule of thumb here is, if it doesn't depend on the switch, move it out of the switch.

    0 讨论(0)
  • 2021-01-17 22:31

    Because that's how it's done in C.

    0 讨论(0)
  • 2021-01-17 22:39

    To answer your "actual question": Why does it continue with the "Creator" case while the case is not "Creator".

    Because you do not have break. Without it, it will continue to the cases below it. The only solution I can think of is putting your default code to the cases, and add break.

    Also, you don't need break on the default case since its the last case in the switch block.

    0 讨论(0)
提交回复
热议问题