Reputation: 48505
I'm using PHP's foreach()
, Sometimes when the inner code doesn't do what i want, I'd like to re-try the same level instead of continuing to the next one.
Is that possible?
Example:
foreach($pics AS $pic){
if(!upload($pic)){
again; // something like this
}
}
Upvotes: 0
Views: 702
Reputation: 1231
function dosomething() {
foreach($pics AS $pic){
if(!upload($pic)){
break;
}
}
$success = true;
}
$success=false;
while( !$success ) dosomething();
While this in theory should work.
I would say absolutely bad programming practice, as you have a good chance of a never ending loop.
Upvotes: 0
Reputation: 141877
No but you can put a while loop inside your loop, this has equivalent behaviour as what you desire above. However you should modify it to use a counter and stop after X many retries to prevent infinite looping.
foreach($pics AS $pic){
while(!upload($pic));
}
Upvotes: 5
Reputation: 3358
You will need to surround your if
with another loop which loops a certain number of times (your maximum retry count), or until you manually break
out of it when your code succeeds.
You could use a goto
I suppose, but that is generally frowned upon, and would do the same thing as an inner loop anyway.
Upvotes: 0