bvtterfly / cycler
An easy way to rotate through several values is to use cycler
Fund package maintenance!
bvtterfly
Requires
- php: ^8.0
Requires (Dev)
- friendsofphp/php-cs-fixer: ^3.0
- pestphp/pest: ^1.20
This package is auto-updated.
Last update: 2024-03-05 14:47:08 UTC
README
🚨 THIS PACKAGE HAS BEEN ABANDONED 🚨
I no longer use PHP and cannot justify the time needed to maintain this package. That's why I have chosen to abandon it. Feel free to fork my code and maintain your own copy.
Cycler : An easy way to rotate through several values
An easy way to rotate through several values is to use cycler, which takes any number of arguments and cycles through them.
$cycle = cycler('odd', 'even'); $numbers = [1,2,3,4,5,6]; foreach ($numbers as $number) { echo "{$number} is {$cycle->next()}"; } // prints 1 is odd 2 is even 3 is odd 4 is even 5 is odd 6 is even
Installation
You can install the package via composer:
composer require bvtterfly/cycler
Usage
Let's say we want to display ads every three posts on our blog's home page. Then we can use cycler
:
$adsCycle = cycler(false, false, true); foreach ($posts as $post) { // show post if ($adsCycle->next()) { // show add } }
You can access the current item using the current
method (in the above example, $adsCycle->current()
)
Testing
composer test
Changelog
Please see CHANGELOG for more information on what has changed recently.
Credits
License
The MIT License (MIT). Please see License File for more information.