php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #8428 continue doesn't pass thru a switch statement
Submitted: 2000-12-26 13:34 UTC Modified: 2010-12-14 11:36 UTC
Votes:1
Avg. Score:2.0 ± 0.0
Reproduced:1 of 1 (100.0%)
Same Version:0 (0.0%)
Same OS:0 (0.0%)
From: ocomte at guarantycity dot com Assigned:
Status: Wont fix Package: *General Issues
PHP Version: 4.0.3pl1 OS: Solaris 7
Private report: No CVE-ID: None
Have you experienced this issue?
Rate the importance of this bug to you:

 [2000-12-26 13:34 UTC] ocomte at guarantycity dot com
in the script :
<?
	for($i=0; $i<5; ++$i)
	{	switch(1)
		{	case 1:
				continue;
		}
		echo "Not printable";
	}
?>

the string is printed 5 times because the continue statement end the switch (like break). In C and C++, and in the PHP manual the continue (unlike break) isn't affected by surrounding switch statement and it seems logical to me.

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2001-01-05 22:33 UTC] zak@php.net
The switch statement is a flow control construct - like for or if.  Because of this continue behaves within switch like it should in any flow control contruct - it skips to the next loop of the construct.  However, switch does not loop - so continue behaves like break.

If you would like to use a continue embedded in a switch statement to continue past a loop in an outside control construct, specify how many levels you would like continue to break out of.

Try these examples to see what I mean:

<pre>
<?php
    for ($i=0; $i<5; ++$i)
    {
        switch(1)
        {
            case 1:
                continue 2;
                break;
        }

        echo "Should be skipped";
    }

    // Should not output anything

    for ($i=0; $i<5; ++$i)
    {
        for ($z=10; $z>5; --$z)
        {
            switch(1)
            {
                case 1:
                    continue 2;
                    break;
            }
            echo "\$z=$z\n";
        }
        echo "\$i=$i\n";
    }
    
    /*
        Should output:
            $i=0
            $i=1
            $i=2
            $i=3
            $i=4
    */
?>



 [2001-01-05 23:04 UTC] zak@php.net
The desired behavior is that continue is transparent to switch statements - this behavior would mimic how if handles continue statements.


 [2010-12-14 11:36 UTC] jani@php.net
-Status: Open +Status: Wont fix -Package: Feature/Change Request +Package: *General Issues
 [2010-12-14 11:36 UTC] jani@php.net
You need to pass the level here:

<?php 

for($i=0; $i<5; ++$i)
{       switch(1)
   {       case 1:
             continue 2;
   }
   echo "Not printable";
}
?>

This code won't output anything. Documented issue also.
 [2015-02-21 19:05 UTC] teo8976 at gmail dot com
The documentation is utterly unclear about this.

See https://bugs.php.net/bug.php?id=69099
 
PHP Copyright © 2001-2017 The PHP Group
All rights reserved.
Last updated: Sun Nov 19 01:31:42 2017 UTC