Skip to content
This repository has been archived by the owner on Feb 5, 2024. It is now read-only.
/ cycler Public archive

An easy way to rotate through several values

License

Notifications You must be signed in to change notification settings

bvtterfly/cycler

Repository files navigation

🚨 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

Latest Version on Packagist Tests Total Downloads

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.

About

An easy way to rotate through several values

Resources

License

Stars

Watchers

Forks

Sponsor this project

Packages

No packages published

Languages