如果某些條件不符合,我試圖跳到循環的下一次迭代。問題是無論如何循環都在繼續。PHP的foreach繼續
我在哪裏出了錯?
更新後的代碼示例以迴應第一條評論。
foreach ($this->routes as $route => $path) {
$continue = 0;
...
// Continue if route and segment count do not match.
if (count($route_segments) != $count) {
$continue = 12;
continue;
}
// Continue if no segment match is found.
for($i=0; $i < $count; $i++) {
if ($route_segments[$i] != $segments[$i] && ! preg_match('/^\x24[0-9]+$/', $route_segments[$i])) {
$continue = 34;
continue;
}
}
echo $continue; die(); // Prints out 34
您正在覆蓋`$ continue`。在循環迭代中完全可以輸入它被設置爲1,然後是2。 – 2010-11-24 18:11:25
在`foreach`之後設置`$ continue = 0;`...我敢打賭你不會再擁有`1`。對於2,你需要告訴它繼續2個級別,所以`繼續2;`,否則它只會去`for`循環的下一個迭代... – ircmaxell 2010-11-24 18:14:44