Home  >  Article  >  Backend Development  >  Example analysis of how to use PHP iterator to implement a Fibonacci sequence function

Example analysis of how to use PHP iterator to implement a Fibonacci sequence function

黄舟
黄舟Original
2017-08-21 11:38:171430browse

Share a PHP iterator to implement a Fibonacci sequence function class. The Fibonacci sequence is usually implemented recursively, but of course there are other methods. Here you can learn and sell now. It is almost not difficult to use PHP iterator to implement a Fibonacci sequence. You just need to rewrite the next() method in the class. The comments have been written into the code and are quite easy to understand.


Step 1: Download the php iterator we need to use in this lesson To implement a Fibonacci sequence function library: http://www.php.cn/xiazai/leiku/768

Step 2: After the download is completed, find the php class file and unzip it to the local server , create a new php file!

Step 3: We call this class in this new file and instantiate this class:

<?php
include_once "code5.php";//引入类文件
$seq = new Fibonacci;    //实例化
$i = 0;
foreach ($seq as $f) {
    echo "$f ";
    if ($i++ === 15) break;
}
?>



##The running results are as shown below:

Example analysis of how to use PHP iterator to implement a Fibonacci sequence function

The above is the detailed content of Example analysis of how to use PHP iterator to implement a Fibonacci sequence function. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn