Skip to content

Instantly share code, notes, and snippets.

@MaheKarim
Last active July 17, 2019 18:43
Show Gist options
  • Save MaheKarim/b056a91ff492c63660646039115b6b19 to your computer and use it in GitHub Desktop.
Save MaheKarim/b056a91ff492c63660646039115b6b19 to your computer and use it in GitHub Desktop.
While Loop In Fibonacci
<?php
/**
* Created by PhpStorm.
* User: Mahe
* Date: 17-Jul-19
* Time: 11:54 PM
*/
function fibonacci ($number){
static $oldValueStore = 0; // store as static var because the value of this var won't change
static $newValueStore = 1;
$counter = 0 ;
while ($counter < $number){
echo " ".$oldValueStore;
$_temporary = $oldValueStore + $newValueStore;
$oldValueStore = $newValueStore;
$newValueStore = $_temporary;
$counter++;
}
} $number = 6; // Input
fibonacci($number);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment