🚨 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.
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
You can install the package via composer:
composer require bvtterfly/cycler
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()
)
composer test
Please see CHANGELOG for more information on what has changed recently.
The MIT License (MIT). Please see License File for more information.