LoginSignup
6

More than 5 years have passed since last update.

posted at

updated at

【PHP】switch文の中でcontinue

結論から書くと他の言語と違い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;
}

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
What you can do with signing up
6