Sometimes it's small things that make a problem solveable with ease. Sometimes you just don't want to study full examples but need small hints, tips and tricks.
Even if there are excellent PHP3 example sites on the web, something between full-blown tutorials and example-scripts was missing. Tips regarding PHP3 had to be gathered either on the mailing lists or on IRC. I hope to address this short-coming with the new web-resource I present in this issue of phpTidbits.
On http://phpwizard.net/tips/ you can browser through all tips currently in the system. There aren't that many as of yet but you're invited to change that. The source of these page is available at http://phpwizard.net/tips/index.phps. Tips are submitted at http://phpwizard.net/tips/submit.html (Source). All pages use some common functions located in http://phpwizard.net/lib.inc.phps.
Fetching a random tip
The main page of phpWizard.net displays a random tip. Until MySQL 3.23, which will support sub-selects, it's a bit of a hassle to fetch a random row of a MySQL-table. The way to do do it with the smallest overhead is like this:
// Select all IDs from the table (ie. the unique identifier for all rows)
$result = mysql_query("SELECT ID FROM tips");
// Init random number generator
srand((double)microtime()*1000000);
// Create a random number between 0 and the count of returned rows
You can even show PHP3 tips on your own website: I've created a page (http://phpwizard.net/tips/tip.html) that you can either fetch with PHP3 and include in your layout or open through a JavaScript pop-up window. Opening files from a remote location is easy in PHP3: fopen() accepts a URL as parameter so you can just use: