Archive for May, 2007

A handy PHP debug function

Thursday, May 3rd, 2007

Like most developers, I often find it useful to spit out a few messages in the middle of a PHP script - especially when I’m debugging some problems.

Sadly, after only a few months our pristine code-base has become spotted with this, over and over again:

if (DEBUG) {
    echo "New user object: "; var_dump($user); echo "\n”;
}

After discovering var_export I felt like I’d improved things by switching to:

if (DEBUG) {
    printf("New user object: %s\n”, var_export($user, true));
}

I still didn’t like the fact that if (DEBUG) was appearing all over the place. So, after stumbling across call_user_func_array I’ve implemented by own debug function:

function debug()
{
    if (DEBUG)
    {
	$args = func_get_args();
	$args[0] = "<pre>" . $args[0] . "</pre>\n";
	for ($i = 1, $l = count($args); $i < $l; $i++) {
	    $args[$i] = htmlspecialchars(var_export($args[$i], true));
	}
	call_user_func_array('printf', $args);
    }
}

Which means I can remove all the if (DEBUG) statements, leaving only:

debug("New user object: %s", $user);

I think that’s much cleaner.

Upgrading WordPress using mysqli

Thursday, May 10th, 2007

Out-of-the-box WordPress assumes it’s going to be talking to a MySQL database using PHP’s original mysql extension instead of the newer mysqli extension (true up to WordPress 2.1.3 at least). If for some reason you need to run using mysqli, there are simple instructions on the WordPress support site: MySQLi for WordPress 2.1.x.

Switching to MySQLi was easy and worked as described, except for one thing: I couldn’t upgrade WordPress. After a bit of sleuthing I found there’s a MySQL version check buried in the upgrade process. For the current version (WordPress 2.1.3) it’s checking that MySQL is version 4.0.0 or higher. According to the PHP documentation for mysqli, it needs to run on MySQL 4.1 or above - so the version check is redundant.

Short-term fix: follow the first few steps of the WordPress upgrade process but before running the upgrade program edit wp-admin/upgrade-functions.php and gut this function:

function wp_check_mysql_version() {
    // (version-check removed)
}

You should be able to run the upgrade program fine now.

UK Petition: Ban the Bog Troll

Thursday, May 31st, 2007

Officially known as bathroom attendants, the bog troll lurks in bars and clubs, interrupting customers’ casual hygiene routine by obstructing sinks and hand-dryers and extorting money for performing such mundane tasks as turning on taps and distributing paper towels.

The bog troll is a menace. The bog troll does not encourage hygiene, it prevents it. Once the third-pint breaks the seal, regular users have no wish to pay an extra pound to offload their processed beer. With typical British manners, bathroom visitors are put in an uncomfortable position: wash their hands tax-free and risk offending the troll; or leave the bathroom unclean, hygiene-be-damned.

Many customers choose the latter. Damn you Bog Troll! Damn you for hoarding the paper towels. Damn you for covering three sinks with CK One. Damn you and your tray of Chupa Chups.

Please help us regulate this scourge, sign the Ban The Bog Troll petition at Downing Street.

This has been a public service announcement. Thank you.

Official Site: Ban The Bog Troll