Last active
December 17, 2015 16:59
-
-
Save Willshaw/5642619 to your computer and use it in GitHub Desktop.
PHP Fibonacci number
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
<? | |
// array to store sequence - initialised with first 2 numbers | |
$nums = array(0,1); | |
// loop 20 times creating more numbers in the sequence | |
for($i = 1; $i <=20; $i++) { | |
$next = $nums[$i]+$nums[$i-1]; | |
array_push($nums,$next); | |
} | |
// output sequence, one number per new line | |
foreach ($nums as $num => $val) { | |
echo '<br />'.$val; | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment