Question

$arr = array('not want to print','foo','bar');

foreach($arr as $item) {
  switch($item) {
      case 'foo':
         $item = 'bar';
         break;
      case 'not want to print':
         continue;
         break;
  }

  echo $item;
}

http://codepad.org/WvW1Fmmo

But "not want to print" is echoed. Why does continue don't apply to the foreach?

Was it helpful?

Solution

From the http://php.net/manual/en/control-structures.continue.php:

Note: Note that in PHP the switch statement is considered a looping structure for the purposes of continue.

So use continue 2; to continue the loop that contains it.

You also have a mismatch between $arr and case. The first word in the array value is no, but you're checking for not in the case.

Corrected codepad

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top