Start New Discussion within our Web Development Community

I'm trying to create a php script to print fibonaaci series. The idea is to add a new number to the series everytime the page is refreshed and print the series.


if (!isset($_COOKIE["fno"]))
if (!isset($_COOKIE["sno"]))
if (!isset($_COOKIE["series"]))
    $text="0 1 ";
    //echo $fibostring;
    $fibostring.=$tno . " ";


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "">
<html xmlns="">
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Fibonacci Series</title>
<body bgcolor="#FFFFFF">
<div style="font-size:20px; color:#cc0000; margin-top:10px"><?php echo $fibostring ?></div>

Check out :
Can anybody tell me what's wrong ?

To add a new fibonacci number on each refresh you should store each previous and current value in a session.


// on the beginning you have 0 and 1
if(!isset($_SESSION['previous']) || !isset($_SESSION['current'])) {

    $_SESSION['previous'] = 0;
    $_SESSION['current'] = 1;

    echo $_SESSION['previous'];
    echo ' ';
    echo $_SESSION['current'];

} else {

    $current = $_SESSION['previous'] + $_SESSION['current'];

    echo $current;

    $_SESSION['previous'] = $_SESSION['current'];
    $_SESSION['current'] = $current;
Nice Answer. Better than my links!

I doubt the OP is still awaiting an answer, but for anybody else -

Agreed - sessions would be the way to go IMO.

I have just noticed that the question is mentioning cookies. Well the principle is the same as using session.

Anyway, it was just nice to do a little brain exercise.

This article has been dead for over six months. Start a new discussion instead.