Created
September 21, 2009 17:43
-
-
Save avdgaag/190399 to your computer and use it in GitHub Desktop.
Cycle function that alternates between arguments on every call.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/* | |
* Cycle between a series of arguments. | |
* | |
* Usage: | |
* | |
* cycle('even', 'odd') # => 'even' | |
* cycle('even', 'odd') # => 'odd' | |
* cycle('even', 'odd') # => 'even' | |
* | |
*/ | |
function cycle() { | |
$args = func_get_args(); | |
$_args = serialize($args); | |
static $currents; | |
if(!isset($currents)) $currents = array(); | |
if(!array_key_exists($_args, $currents)) { | |
$currents[$_args] = 0; | |
} else { | |
$currents[$_args]++; | |
} | |
$index = $currents[$_args] % count($args); | |
return $args[$index]; | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment