PHP Challenge 1 - Generate the Fibonacci Numbers Based on User Input

In this challenge you are required to allow the user to input a number. You will then use this number to generate the Fibonacci numbers.

The Fibonacci numbers are 0, 1, 1, 2, 3, 5, 8, 13, 21, ...etc

The next number in the sequence is found by adding the 2 previous numbers.

e.g. the 5th number in the sequence is 3 and is found by adding the 2 previous numbers together i.e. 1 + 2 = 3.

Develop a simple HTML form to allow the user to enter a number to represent the number of Fibonacci numbers to find.

Then develop a PHP script to echo out these Fibonacci numbers as a list separated by commas.

e.g. if the user enters 5 then the output needs to be:

0, 1, 1, 2, 3

NOTE: does your list have a trailing comma? (i.e. a comma at the end of the list).  If so, can you find a way to remove this?

Would you like the solution to this challenge?

The full video solution together with downloadable code is available in the PHP for Beginners course.

This course is taught by Patrick Morrow, a national award-winning teacher, who is very active in the course and who provides full support.

To enrol in the course click here.


Your email address will not be published.

User Avatar

Will you be adding more challenges?

by LittleJ, 29-Dec-2015

User Avatar

@LittleJ yes, I will be adding lots more challenges. Some are fairly simple and aimed at coding beginners. Others require much more thought and logical application. Full solutions are available in my online courses should you opt to enrol.

by Patrick Morrow, 29-Dec-2015

PHP for Beginners

Would you like lifetime access to the hugely successful PHP for Beginners course? Click here to find out how.

FREE Code Snippets

FREE hints and tips delivered direct to you once per month.