Skip to content

Instantly share code, notes, and snippets.

@HelgeSverre
Created February 15, 2017 07:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save HelgeSverre/75088df52b4cf28048171eeeb8af7f92 to your computer and use it in GitHub Desktop.
Save HelgeSverre/75088df52b4cf28048171eeeb8af7f92 to your computer and use it in GitHub Desktop.
Simple Retrying for loop example
<?php
$retries = 0;
$maxRetries = 3;
// Should fail this amount of times
$failCounter = 5;
for($i = 0; $i < 10; $i++) {
var_dump($i);
// Induce failure
if ($failCounter > 0) {
$failCounter--;
var_dump("FAILED");
// Failed, retry
if ($retries < $maxRetries) {
$i--; // Reset index back to the failed index
$retries++;
} else {
var_dump("Failed max times");
$retries = 0;
continue;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment