Fibonacci in PHP

So here is my second post in the Fibonacci project. For those of you that have not seen the first post here, the Fibonacci project is a learning experiment designed to highlight the similarities and differences between programming languages. In our C Fibonacci example, we learned that we need to declare variables and types, even the return type of a function. In this PHP example, we’ll see that PHP, while different, is very similar to C in syntax.

You’ll notice a couple of things about this version of the Fibonacci program. Because it’s intended to be run on the command line, the first line tells the shell which interpretor to use. In addition, I have been careful here to return an exit code so that the shell knows that the program completed successfully.Stay tuned for our next installment, I’ll keep it a surprise, and even though everyone thinks it will be Ruby, I’m going to save that one for later.

7 Responses to “Fibonacci in PHP”

  1. Charles Says:

    Not sure whether it’s me (hey, it wouldn’t be the first time) but this script stops working properly after generating the 47th number – 48th number should be 2971215073 but I get -1323752223 instead. If I change the script so $a gets echoed (rather than use printf) I get the correct result. Is this a limitation with printf or something? Anyway, keep up the good work!

  2. raiser Says:

    the error: 32 bit is the bug.
    I’ve a fibunacci-code-function, as limit in max_execution_time.

    I’ve testet my code on a 1.2 ghz intel mobile witch 1GB memory and have a maximum of numberlength with 1012.
    It ist many more as a 32Bit Integer.

  3. raiser Says:

    my code was written in php,
    but this can translate in “c” ;)

    mfg

  4. pavan kailasapu Says:

    it is also fibanacci code it will definitely help full to all
    <?php
    $first = 0;
    $second = 1;
    $n =20;
    print $first.'';
    for($i=1;$i<=$n-1;$i++)
    {
    $final = $first + $second;
    $first = $second;
    $second = $final;
    print $final.'';
    }
    ?>

  5. vijayakumar k Says:

    <?php

    function fib1($str)
    {
    $fib=Array();
    $fib[0]=0;
    $fib[1]=1;

    for($i=2;$i

  6. vijayakumar k Says:

    This code will surely work…..

    <?php

    function fib1($str)
    {
    $fib=Array();
    $fib[0]=0;
    $fib[1]=1;

    for($i=2;$i

  7. yann Says:

    i’m grateful