3
$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?

jakxnz
  • 550
  • 5
  • 21
Toni Michel Caubet
  • 19,333
  • 56
  • 202
  • 378

1 Answers1

7

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

Barmar
  • 741,623
  • 53
  • 500
  • 612
  • Thanks for the heads up, didn't ever heard of it. Actually my code was larger and there where no typos, I tried to reproduce a simpler version of the code, haha. Thanks again! – Toni Michel Caubet Oct 26 '13 at 12:33