17
6

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

【PHP】switch文の中でcontinue

Last updated at Posted at 2015-07-15

結論から書くと他の言語と違いswitch文中のcontinueはswitch文を対象にする。

ループの中で、該当なしならスキップしたい場合にcontinueを使うと、switch文を抜けるだけでループをスキップしない。

ループ内でスキップしない
foreach($member as $name){
  switch($name) {
    case 'suzuki':
      $score = '70';
    case 'sato':
      $score = '85';
    defalt:
      continue;
  }

  echo $score;
}

continueがswitch文を対象にするので、この場合continue 2とするのが正解。

ループ内でスキップする
foreach($member as $name){
  switch($name) {
    case 'suzuki':
      $score = '70';
    case 'sato':
      $score = '85';
    defalt:
      continue 2;
  }

  echo $score;
}
17
6
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
17
6

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?