Jyrone Parker

Jyrone Parker Live – Buidling Our First PHP Application (Fibonacci)

What Better Assignment Than Fibonacci?

Since I am finished with the beginning into to PHP videos I found it imperative to do a stream putting everything together and showing you how easy it is to write an app. In today’s live stream I create a Fibonacci app, for those who are not familiar with the Fibonacci sequence there is a great wiki here. In this application I demonstrate variables, control flow logic, exception handling, and function design. All my code can be seen on my Github here or you can view it below in text or on video

<?php

$input = '';
$running = true;
while($running){
try{
getInput($input);
echo calculateFib($input) . "\n";
}
catch(Exception $e){
echo "Message: {$e->getMessage()}";
}
}

function getInput(&$input){
$input = readline("Please input your number or q to quit: ");
};

function calculateFib($input){
if(intval($input) < 0){
throw new Exception("Input must be greater than zero! \n");
}
elseif($input === 'q' || $input === 'Q'){
exit();
}
if(intval($input) === 0 || intval($input) === 1){
return 1;
}
else{
return calculateFib($input-1) + calculateFib($input - 2);
}
};
?>

 

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Instagram

Follow Me!

Free Email Updates
Get the latest content first as well as EXCLUSIVE gifts, offers and discounts.
We respect your privacy.
%d bloggers like this: