Cycle - Cycles through arguments
2
Use this to print alternating values from an array.
It cycles through a series of values based on an iteration number.
For example, you could use this for alternating background colors.
It cycles through a series of values based on an iteration number.
For example, you could use this for alternating background colors.
<?php
/**
* Use this to print alternating values from an array.
* It cycles through a series of values based on an iteration number.
* For example, you could use this for alternating background colors.
*/
Class Cycle {
var $options;
var $num_options;
var $idx;
public function __construct()
{
$this->options = func_get_args();
$this->num_options = count($this->options);
if( is_array($this->options[0]) )
{
$this->options = $this->options[0];
$this->num_options = count($this->options);
}
$this->idx = 0;
}
public function __toString()
{
$ret = $this->options[$this->idx++];
if( $this->idx >= $this->num_options ) $this->idx = 0;
return $ret;
}
}
//usage
$cycle = new Cycle('red','green','blue');
echo $cycle; //red
echo $cycle; //green
echo $cycle; //blue
echo $cycle; //red
//outputs redgreenbluered
?>






There are currently no comments for this snippet.