如何跳出循环但在下一次迭代中继续?

时间:2014-06-11 09:27:18

标签: php

在PHP中,break退出给定点的循环。但是有可能强制循环跳转到给定点的下一次迭代,而不是完全退出吗?基本上:

for ($i = 0; $i < $foo; $i++){  
    if ($i == 1){    
        gotoNextIteration;  
    } else {    
        //do something else   
    } 
}

3 个答案:

答案 0 :(得分:5)

为此目的使用continue

for ($i = 0; $i < $foo; $i++){
  if ($i == 1){
    continue;
  } else {
    //do something else
  } 
}

答案 1 :(得分:0)

使用continue;或者您也可以这样做

for ($i = 0; $i < $foo; $i++){
  if ($i != 1){
     //do something
  } 
}

你不需要任何其他东西。

答案 2 :(得分:-1)

是的..我为动态内容构建了许多类型的循环:

for ($i = 1; $i <= 20; $i++) {
    if ($i == 1) {
        // write table header
    } else if ($i == 20) {
        // write the table footer
    } else {
        // fill the table columns
    }
}

这是一个基本示例,但我将其用于数据迭代(如图像库)和动态表等。但我向Abhik Chakraborty的答案低头,因为我不熟悉“继续”。我喜欢学习新东西。

相关问题