PHP Classes

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 (1)    
Ratings Unique User Downloads Download Rankings
Not yet rated by the usersTotal: 80 This week: 1All time: 10,084 This week: 560Up
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.

Innovation Award
PHP Programming Innovation award nominee
November 2021
Number 4
Many applications need to execute tasks a given number of times or until a specific condition happens.

This class allows developers to execute a task in a cycle by passing a given callback function.

The class can call the function a given number of times or stop when the called function calls this class to stop the cycle.

This possibility allows developers to have more control over when the task functions can decide to stop the cycle loops.

Manuel Lemos
Picture of Isa Eken
  Performance   Level  
Name: Isa Eken <contact>
Classes: 20 packages by
Country: Turkey Turkey
Age: 21
All time rank: 276644 in Turkey Turkey
Week rank: 52 Up2 in Turkey Turkey Up
Innovation award
Innovation award
Nominee: 13x

Documentation

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:80
This week:1
All time:10,084
This week:560Up