Warning: “continue” Targeting Switch Is Equivalent To “break”. Did You Mean To Use “continue 2”?

This warning is displayed in PHP versions 7.2 and later. It indicates that the use of the continue statement within a switch statement may lead to unexpected behavior.

In previous versions of PHP, the continue statement used within a switch statement would behave differently. It would continue to the next case. However, since PHP 7.2, the behavior has changed, and the continue statement now behaves like a break statement, which exits the switch block.

To fix this warning, you have a couple of options:

  1. If you want the continue statement to behave as before (continuing to the next case), you can use continue 2 instead. This will skip the remaining code within the current iteration of the switch block and move to the next iteration.

  2. If you want to exit the switch block completely, you can use break instead. This will exit the switch block and continue with the code after the switch statement.

Here’s an example to illustrate these solutions:

switch ($variable) {
    case 1:
        // Do something
        continue;       // Warning! Change this to "continue 2" or "break"
    case 2:
        // Do something else
        break;
}

Remember to choose the appropriate fix based on your desired behavior within the switch statement.

About the Author Rex

I'm a passionate tech blogger with an insatiable love for programming! From my early days tinkering with code, I've delved into web dev, mobile apps, and AI. Sharing insights and tutorials with the world is my joy, connecting me to a global community of like-minded tech enthusiasts. Python holds a special place in my heart, but I embrace all challenges. Constantly learning, I attend tech conferences, contribute to open-source projects, and engage in code review sessions. My ultimate goal is to inspire the next generation of developers and contribute positively to the ever-evolving tech landscape. Let's code together!