I just got bit by assuming the following:
foreach ($arr as $key => $value) {
switch($key) {
// ... some other cases
default:
continue;
// ^== assumption: move on to the next iteration of the foreach
// actual PHP: treat this continue just like a break
}
// ...
}
But in fact, according to the documentation for continue:
the switch statement is considered a looping structure for the purposes of continue.
Is there a reason for this choice on the part of PHP language designers? As far as I can tell, switch isn’t a looping control structure, so why treat it like one in this case?
I think you won’t find any real “reason” for this behavior.
The only real motivation behind this behavior was probably that implementing
switchas if it were a looping structure allows PHP to reuse existingbreakandcontinuesemantics of loops instead of reimplementing a special version forswitch.Or to phrase it more positively: It’s for consistency.