Last active

Embed URL

HTTPS clone URL

SSH clone URL

You can clone with HTTPS or SSH.

Download Gist
View continue-switch-in-loop-weirdness.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68
<?php
$arr = array(1, 2, 3);
/**
* PHP Weirdness 1
* 'continue' acts like 'break' inside of a switch statement when inside of a loop
* whereas 'continue' acts as expected inside an elseif block
*/
foreach ($arr as $item) {
switch($item) {
case 1:
echo 'one';
break;
case 2:
echo 'two';
continue;
case 3:
echo 'three';
break;
}
echo ' wasn\'t skipped' . PHP_EOL;
}
/**
* OUTPUT:
* one wasn't skipped
* two wasn't skipped
* three wasn't skipped
*/
// -------- VS. ---------- //
foreach ($arr as $item) {
if ($item == 1) {
echo 'one';
} elseif ($item == 2) {
echo 'two';
continue;
} elseif ($item == 3) {
echo 'three';
}
echo ' wasn\'t skipped' . PHP_EOL;
}
/**
* OUTPUT:
* one wasn't skipped
* twothree wasn't skipped
*/
// -------- FIX! ---------- //
foreach ($arr as $item) {
switch($item) {
case 1:
echo 'one';
break;
case 2:
echo 'two';
continue 2;
case 3:
echo 'three';
break;
}
echo ' wasn\'t skipped' . PHP_EOL;
}
/**
* end Weirdness 1
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.