From the course: Learning PHP
Unlock the full course today
Join today to access over 22,400 courses taught by industry experts or purchase this course individually.
Solution: Calculating the Fibonacci sequence - PHP Tutorial
From the course: Learning PHP
Solution: Calculating the Fibonacci sequence
(upbeat music) - [Instructor] All right, so here's the solution to the challenge of printing every number in the Fibonacci sequence without going over 200. And we put a limit on there because the Fibonacci sequence goes on infinitely, right? So there are a lot of solutions to this problem, this is a pretty common computer science problem and therefore, a lot of beginner programmers think about it. But we're going to do this in a very straight forward way. So we're going to use three variables here, current, which we'll start at one because that's where the Fibonacci sequence starts, previous, which is going to be zero because we're going to need to add some previous value to the first entry and next, which we'll set to null for now. Then we'll have a while loop. We'll say while current is less than 200 and we can always put the limit in a variable as well, so we could say limit gets 200 just in case we ever want to…
Practice while you learn with exercise files
Download the files the instructor uses to teach the course. Follow along and learn by watching, listening and practicing.