PHP code example of jralph / retry

1. Go to this page and download the library: Download jralph/retry library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

jralph / retry example snippets


mixed retry (int|callable $attempts , callable $command [, callable $onError = null])



use function Jralph\Retry\retry;

$result = retry(3, function (int $attempt) {
    // Throwing an error as an example....first 2 attempts will fail.
    if ($attempt < 2) {
        throw new Exception('Just throwing an error as an example!');
    }
    
    return 'Hello World!';
});

// Outputs 'Hello World!'
echo $result;



use Jralph\Retry\Retry;
use Jralph\Retry\Command;

$retry = new Retry(new Command(function (int $attempt) {
    // Throwing an error as an example....first 2 attempts will fail.
    if ($attempt < 2) {
        throw new Exception('Just throwing an error as an example!');
    }
    
    return 'Hello World!';
}));

$result = $retry->attempts(3)->run();

// Outputs 'Hello World!'
echo $result;