Problem with switch syntax in PHP?

Source: Internet
Author: User
switch ($para1) {            case 1:                return self::otherFunction($para2);                break;        }

example, if Case 1 is met, then does the last break still need to be?

Reply content:

switch ($para1) {            case 1:                return self::otherFunction($para2);                break;        }

example, if Case 1 is met, then does the last break still need to be?

No, break is unnecessary if you use return as the end of the case.

There's definitely no need for the following break to return.

According to the landlord's writing, break will not need, because the program in return when the jump, the code behind will not be executed, so break has no relationship.

From the code of the caption, break is not required because the return code is no longer executed.

Plus break is never wrong, and sometimes it goes wrong without a break. It is recommended to add break to any situation so that other small partners working together can better understand the code.

If you have only one option, of course not, if there are other case options, it is required, otherwise the program will execute sequentially to the following option until a break or end is encountered, and sometimes this feature will be applied to handle the same handling of multiple options. That is, in multiple cases no write processing and break, only the last case to add the processing mode.
Comments do not post code, I modify: The following example:

switch( $i ){   case 1:   case 2:   case 3:       $c =555;       break;   default:       $c =2;}

You can try this example ~

PHP is the best language in the world

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.