Recent Topics

Warning: "continue" targeting switch is equivalent to "break"

Started by on Jun 17, 2019 – Contents updated: Jun 24, 2019

Jun 17, 2019 16:05    

The full warning is

Warning: "continue" targeting switch is equivalent to "break". Did you mean to use "continue 2"? in /home/calstock/public_html/inc/items/items.ctrl.php on line 338.

Warning: "continue" targeting switch is equivalent to "break"

This happened once when I tried to edit a comment see

Complete URL of where the problem appears :
Name or URL of your webhost
Exact b2evolution version number :6.11.2

Jun 17, 2019 22:06

I have commented out line 338 in /inc/items/items.ctrl.php and all seems fine. I'm not sure why 'continue' is used as there is no code to continue to before the closing bracket. Lools like an imported code that was used for posts and left from the ancient days??

continue resumes execution just before the closing curly bracket ( } ), and break resumes execution just after the closing curly bracket.

                   if( $new_Chapter->dbinsert() !== false )
						{ // Category is created successfully
							$Messages->add_to_group( sprintf( T_('New category %s created.'), '<b>'.$new_categories[ $fileNum ].'</b>' ), 'success', T_('Creating posts:') );
						{ // Error on creating new category
							$Messages->add( sprintf( T_('New category %s creation failed.'), '<b>'.$new_categories[ $fileNum ].'</b>' ), 'error' );
							//continue; // Skip this post
						$cat_Array[ $fileNum ] = $new_Chapter->ID;

Jun 23, 2019 10:59

So to summarise.

In PHP, if continue is applied to a switch statement, it behaves the same as break. In other languages, it would continue the surrounding loop instead. To avoid confusion, this RFC proposes to deprecate and remove continues acting on switch.

As since PHP 7.3.* there is a warning for each instance I'm replacing all instances of continue; with break;

And for those interested I will update the list of files here but not the version or line numbers.

So the list so far is

    • /inc/items/items.ctrl.php
    • /inc/collections/_search.funcs.php
    • /plugins/_bbcode.plugin.php
    Searched a dozen or so files using the [switch] statement but all use break;

Jun 23, 2019 11:04

Further Notes

Backward Incompatible Changes
PHP 7.3 throws additional deprecation warnings, PHP 8 generates a compile error. Fixing it is always trivial by replacing continue with break.

Unaffected PHP Functionality
Continue can still be used inside switch statements, as long as it does not target the switch. The meaning of continue and break inside switch never changes, some cases are just forbidden.

To further clarify which uses of continue are affected, please consider the following example:

while ($foo) {
    switch ($bar) {
        case "baz":
            while ($xyz) {
                continue;   // Targeting the inner while loop: Allowed
                continue 2; // Targeting the switch: Deprecated
                continue 3; // Targeting the outer while loop: Allowed

This post has 1 feedback awaiting moderation...

Form is loading...

Responsive CMS – This forum is powered by b2evolution CMS, a complete engine for your website.