Page 200 - Beginning PHP 5.3
P. 200
Part II: Learning the Language
function fibonacci( $n ) {
if ( ( $n == 0 ) || ( $n == 1 ) ) return $n;
return fibonacci( $n-2 ) + fibonacci( $n-1 );
}
for ( $i=0; $i <= $iterations; $i++ )
{
?>
<tr<?php if ( $i % 2 != 0 ) echo ‘ class=”alt”’ ?>>
<td>F<sub><?php echo $i?></sub></td>
<td><?php echo fibonacci( $i )?></td>
</tr>
<?php
}
?>
</table>
</body>
</html>
Save the script as fibonacci_recursion.php in your document root folder and run it via your Web
browser. You should see a page much like Figure 7-6. Notice that the sequence is identical to that
produced by the script in Chapter 4.
Figure 7-6
162
9/21/09 9:00:58 AM
c07.indd 162 9/21/09 9:00:58 AM
c07.indd 162