The Fibonacci sequence is usually done recursively, and of course there are other methods. Now learn to sell, use PHP iterator to implement a Fibonacci sequence, almost no difficulty, but the class of the next () method rewrite once. Comments have been written into the code and are fairly well understood.
Current
}
Public Function key () {
return $this->key;
}
Public function next () {
The key is here.
Saves the current value to the $newprevious
$newprevious = $this->current;
Assigns the previous value and the current value to the current value
$this->current + + $this->previous;
Previous value assigned to last
$this->previous = $newprevious;
$this->key++;
}
Public Function Rewind () {
$this->previous = 1;
$this->current = 0;
$this->key = 0;
}
Public Function valid () {
return true;
}
}
$seq = new Fibonacci;
$i = 0;
foreach ($seq as $f) {
echo "$f";
if ($i + + = =) break;
}
?>
Program Run Result:
0 1 1 2 3 5 8 13 21 34 55 89 144 233-377 610