php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #33034 continue behavior in switch has a potential use
Submitted: 2005-05-15 03:35 UTC Modified: 2015-06-19 13:40 UTC
Votes:3
Avg. Score:3.0 ± 1.6
Reproduced:2 of 3 (66.7%)
Same Version:2 (100.0%)
Same OS:2 (100.0%)
From: sadmac at earthlink dot net Assigned: kalle (profile)
Status: Closed Package: *General Issues
PHP Version: 4.3.11 OS: Windows XP
Private report: No CVE-ID: None
 [2005-05-15 03:35 UTC] sadmac at earthlink dot net
Description:
------------
When the user uses continue in a switch, it behaves essentially the way a break behaves. However, the name might imply (and I already have use for this feature) that the switch statement would then continue evaluating cases, and execute any code under another case statement further down in the switch which fit the condition.

Reproduce code:
---------------
<?php

$j = 0;

switch($j)
{
   case 0:
       echo "part one ";
       break;
   case 1:
       //blah blah blah
       break;
   case 0:
       echo "part two";
       break;
} //Will only ouput "part one"

switch($j)
{
   case 0:
       echo "part one ";
       continue;
   case 1:
       //blah blah blah
       continue;
   case 0:
       echo "part two";
       continue;
} //Should potentially output "part one part two"

?>

Expected result:
----------------
The first statement should behave as it currently would, the second should evaluate both case 0s, not just the first.

Actual result:
--------------
Both switches evaluate only the first case 0.

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2008-03-07 12:55 UTC] packard_bell_nec at hotmail dot com
I also like this feature proposal, because it can simplify the coding VERY much. It can eliminate the repeating checkups, statements, or even functions. In addition, it can increase the readability.
 [2015-06-19 13:40 UTC] kalle@php.net
-Status: Open +Status: Closed -Package: Feature/Change Request +Package: *General Issues -Assigned To: +Assigned To: kalle
 [2015-06-19 13:40 UTC] kalle@php.net
You can omit the break statement to fall-through to the underlaying segment, but I don't see how it can be possible in a reasonable way to achieve skipping certain segments in the switch statement.

Although I'm not sure why you would want to achieve your example with a switch-statement, and not a simple if condition, since if what happens in 'case 0' (top part) doesn't have anything to do with what happens in 'case 1' and jumps straight pass it, if you absolutely need that logic to be shared, then it should be done in a function call or something, as of 5.3 you can even use goto! (no joke intended)
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Fri May 17 02:01:32 2024 UTC