PHP Loop Class: Execute a function a given number of times

Recommend this page to a friend!
  Info   View files Documentation   View files View files (16)   DownloadInstall with Composer Download .zip   Reputation   Support forum   Blog    
Last Updated Ratings Unique User Downloads Download Rankings
2021-10-31 (6 days ago) RSS 2.0 feedNot yet rated by the usersTotal: 33 This week: 33All time: 10,304 This week: 8Up
Version License PHP version Categories
loops 1.0MIT/X Consortium ...8.0PHP 5, Language
Description Author

This class can execute a function a given number of times.

It can take as parameter a callback function and a number that specified the length of loop cycle.

The class can execute the callback function until it reaches the specified loop cycle length.

Picture of Isa Eken
  Performance   Level  
Name: Isa Eken <contact>
Classes: 14 packages by
Country: Turkey Turkey
Innovation award
Innovation award
Nominee: 8x

Details

Loops

Latest Version on Packagist GitHub Tests Action Status GitHub Code Style Action Status Total Downloads

Installation

You can install the package via composer:

composer require isaeken/loops

Usage

Basic Usage

loop(5, function ($loop, $instance) {
    return $loop->odd;
}); // [false, true, false, true, false]

Get current loop

loop(2 ,function ($loop, $instance) {
    return [
        'iteration' => $loop->iteration,
        'index'     => $loop->index,
        'remaining' => $loop->remaining,
        'count'     => $loop->count,
        'first'     => $loop->first,
        'last'      => $loop->last,
        'odd'       => $loop->odd,
        'even'      => $loop->even,
    ];
});
// [
//  [
//    'iteration' => 0,
//    'index' => 0,
//    'remaining' => 1,
//    'count' => 2,
//    'first' => true,
//    'last' => false,
//    'odd' => false,
//    'even' => true,
//  ],
//  [
//    'iteration' => 1,
//    'index' => 1,
//    'remaining' => 0,
//    'count' => 2,
//    'first' => false,
//    'last' => true,
//    'odd' => true,
//    'even' => false,
//  ]
// ]

Break the loop

loop(3, function ($loop, $instance) {
    if ($loop->index > 1) {
        $instance->stop();
    }
    
    return $loop->index;
}); // [0, 1]

Loop random times

loop_random(function ($loop, $instance) {
    return $loop->index;
}); // executed random times.

$min = 5;
$max = 10;

loop_random(function ($loop, $instance) {
    return $loop->index;
}, $min, $max);

Changelog

Please see CHANGELOG for more information on what has changed recently.

Contributing

Please see CONTRIBUTING for details.

Security Vulnerabilities

Please review our security policy on how to report security vulnerabilities.

Credits

License

The MIT License (MIT). Please see License File for more information.

  Files folder image Files  
File Role Description
Files folder image.github (3 files, 2 directories)
Files folder imagesrc (2 files)
Files folder imagetests (1 file)
Plain text file .editorconfig Data Auxiliary data
Plain text file .php_cs.dist.php Aux. Auxiliary script
Plain text file CHANGELOG.md Data Auxiliary data
Plain text file composer.json Data Auxiliary data
Plain text file LICENSE.md Lic. License text
Plain text file phpunit.xml.dist Data Auxiliary data
Plain text file README.md Doc. Documentation

  Files folder image Files  /  .github  
File Role Description
Files folder imageISSUE_TEMPLATE (1 file)
Files folder imageworkflows (2 files)
  Plain text file CONTRIBUTING.md Data Auxiliary data
  Plain text file FUNDING.yml Data Auxiliary data
  Plain text file SECURITY.md Data Auxiliary data

  Files folder image Files  /  .github  /  ISSUE_TEMPLATE  
File Role Description
  Plain text file config.yml Data Auxiliary data

  Files folder image Files  /  .github  /  workflows  
File Role Description
  Plain text file php-cs-fixer.yml Data Auxiliary data
  Plain text file run-tests.yml Data Auxiliary data

  Files folder image Files  /  src  
File Role Description
  Plain text file helpers.php Aux. Auxiliary script
  Plain text file Loop.php Class Class source

  Files folder image Files  /  tests  
File Role Description
  Plain text file LoopTest.php Test Unit test script

 Version Control Unique User Downloads Download Rankings  
 100%
Total:33
This week:33
All time:10,304
This week:8Up
For more information send a message to info at phpclasses dot org.