Exercise : Fibonacci (by Rishi Goutam)

Modify the following HTML to print the first 20 Fibonacci numbers in an ordered list using PHP. Recall that in the Fibonacci sequence, each number is the sum of the two previous numbers, with the first and second numbers being 0 and 1.

<!DOCTYPE html>
<html>
	<head>
		<title>Fibonacci Sequence</title>
	</head>
	<body>
		<h1>The first twenty Fibonacci numbers:</h1>

		<!-- (your code here!) -->
	</body>
</html>

Exercise solution

	...
		<h1>The first twenty Fibonacci numbers:</h1>
		<?php
		$first = 0;
		$second = 1;
		for ($i = 0; $i < 20; $i++) {
			?>
			<li><?= $first ?></li>
			<?php
			$temp = $second;
			$second += $first;
			$first = $temp;
		}
		?>