开发者

Skip loop iteration from called function in PHP

I have a while loop that calls a function that checks some stuff from the database. How can I continue; the while loop from this function? Simply calling continue does not work.

The problem is that this is a plugin for an existing application, so, while I could put my code into the while loop for me, I need to keep the code i开发者_如何学JAVAn this function in order to easily distribute it.

Any suggestions?


well what you can do is something like

while(condition){
   ... do your stuff ...
   $continue = shouldContinue();
   if($continue){
     ... do your stuff ...
   }
 }

where shouldContinue is a function that returns true if you want it to continue or false otherwise. I am not sure though what is your purpose. If you provided more information then it would be easier to help


There is no way to do this. I just wrote this:

for($i = 0; $i < 10; $i++){
    // dummy loops
    while(1){ 
        while(1){
            break test($i)+1;
            break;
        }
        // Do all the stuff that would normally be in the for loop
        echo $i;
        break;
    }
}

function test($i){
    if($i == 5)
        return 1;
}

Then I realized it's just a really really ugly version of @mkk's answer: http://codepad.org/tzciEeJN

You can only use return to exit a function, and break an continue in switch statements / loops.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜