Skip to content

Instantly share code, notes, and snippets.

@avdgaag
Created September 21, 2009 17:43
Show Gist options
  • Save avdgaag/190399 to your computer and use it in GitHub Desktop.
Save avdgaag/190399 to your computer and use it in GitHub Desktop.
Cycle function that alternates between arguments on every call.
<?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