PHP Coding styles return; in switch/case

后端 未结 6 2071
栀梦
栀梦 2021-02-01 00:13

we\'re trying to implement new coding style guidelines for our team, the php codesniffer is printing an warning on switch case statements when no \"break\" is found like:

<
6条回答
  •  灰色年华
    2021-02-01 01:05

    From the PHP manual (http://us3.php.net/manual/en/control-structures.switch.php) :

    PHP continues to execute the statements until the end of the switch block, or the first time it sees a break statement. If you don't write a break statement at the end of a case's statement list, PHP will go on executing the statements of the following case. For example:

    
    

    Here, if $i is equal to 0, PHP would execute all of the echo statements! If $i is equal to 1, PHP would execute the last two echo statements. You would get the expected behavior ('i equals 2' would be displayed) only if $i is equal to 2. Thus, it is important not to forget break statements (even though you may want to avoid supplying them on purpose under certain circumstances).

提交回复
热议问题