It is a hacker tradition to write a program that shows a random proverb or fortune. This goes back to the day of UNIX terminals that would run programs automatically upon login. You could set up your terminal to show you a random funny, motivational, sarcastic, or inspirational thought. We did this back in my days at UC Santa Cruz in the early 1990s.
Not so many people log into UNIX terminals daily like this now, although, to be honest, I'm currently logged into the Mac Terminal program and I'm typing this code in vi, my favorite text editor. So if I had the fortune program installed, I could run it. Instead, we'll display the fortune on a web page. It could be at the bottom of your personal home page, or it could be a separate page set apart for the purpose of only displaying a fortune.
A random fortune program consists of two parts: (1) a database of sayings, and (2) a program that chooses one saying at random to display on the screen. The program is actually pretty simple. The database can range from a dozen sayings to literally thousands.
We make this program in my PHP course at Mission College and also in the JavaScript portion of my Mobile App Design course at Silicon Valley CTE (high school). We could code this in any language, but what are the tradeoffs?
PHP
- PRO. The database file can be a straight text file. This means we can add new sayings to it at any time without changing any code.
- CON. The PHP code must be run on a web server, such as WAMP, MAMP, or the native Mac Apache web server. One might also be able to run an IIS server on a Windows machine, but it may be less cumbersome to just run WAMP.
- PRO. Since we only download one fortune at a time, as the database gets larger, we still download only one fortune, not the whole database.
- CON. Each new fortune request requires that the whole page be downloaded from the server again.
JavaScript
- PRO. The JavaScript code can be embedded into an HTML web page and run locally using the file: protocol. No web server is needed.
- PRO. The simplicity of the JavaScript implementation and lack of any need for software installation means we can teach this to high school students who don't have much computer knowledge.
- CON. Since JavaScript cannot read text files, we must code the proverbs as a JavaScript data array. This is not difficult, but every time one adds a fortune to the database, there is the possibility of error, particularly with quotation marks and commas.
- CON. As the database gets larger, so will the size of the file we have to download to display only one fortune. Of course, if the user chooses to display multiple fortunes, no further downloads are necessary after the first.
AJAX
Ajax (Asynchronous JavaScript with XML), if implemented correctly, can combine the best features of both PHP and JavaScript. With Ajax, we make the front-end (display and user interaction) using JavaScript, but we make the back-end (database) with PHP. When the user requests a new fortune, we request one from the server, and we transfer only the new fortune, not the whole page.
- CON. The code cannot be run locally using the file: protocol, but must be run from a web server.
- PRO. Since we only download one fortune at a time, as the database gets larger, we still download only one fortune, not the whole database.
- PRO. The database file can be a straight text file. This means we can add new sayings to it at any time without changing any code.
- PRO. We can display a new fortune without reloading the whole page.