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.
#!/usr/bin/php -q <?php /** * This is our main function. In PHP, there isn’t really a need for this to be * a function, but to keep consistant, I’ve placed it there anyway, and called * it from the script. In keeping with good programming practices, this script * exits with a status 0. Any other status would indicate an error condition. */ function main(){ printf("How many numbers of the sequence would you like?\n"); /* * To read from stdin in php, you open it as a file handle, and use * the standard file reading functions */ $fr=fopen("php://stdin","r"); $n = rtrim(fgets($fr,128)); fclose ($fr); /* Then we call the fibonacci function */ fibonacci($n); exit(0); } /** * The fibonacci function should look familiar. */ function fibonacci($n){ /* * Notice that in PHP, we do not have to declare our variables, nor must we * declare their types. Also see how variables in PHP are prefaced with the * dollar sign. Other than that, this is identical to the C version of this function. */ $a = 0; $b = 1; for ($i = 0; $i < $n; $i++){ printf("%d\n",$a); $sum = $a+$b; $a = $b; $b = $sum; } } /** * In PHP, program execution starts here. It isn’t neccessary to place functions * ahead of the program, but it’s a good practice. In a larger application, these * functions would be in seperate files. Again, since PHP is a procedural language, * I could have simply included the contents of the main function here, rather than * defining a function and calling it. */ main(); ?>
HTML code generated by vim-color-improved v.0.3.2.Download this code: fibonacci.php
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.