The whitespace of death

As the first entry to the list of my PHP-related WTFs, I present:

**The whitespace of death**

This one has taken me almost a **full hour** to sort out. There may be some good features to PHP, yet this one I don’t think is even one. It should be considered a bug. As my experience has grown in [reporting bugs myself][php-bug1] or trying to [report a bug][php-bug2], I reconsidered and chose not to report it as a “bug”.

[php-bug1]: https://bugs.php.net/bug.php?id=48458
[php-bug2]: https://bugs.php.net/bug.php?id=51112

The whitespace of death
=====

Once a customer reported to us, that a crucial PHP function (namely `imagecolorallocate()`) was “missing” from one of our shared hosting servers. It was, in fact, not. We made sure, that for all versions of PHP available on our systems, `phpinfo()` would say that gd2 was in fact loaded and provided its basic functions.

I went on to take a look at the customer’s script and found nothing suspicious. It looked sort of like this:

// code before
$color = imagecolorallocate($img, 0xFF, 0x00, 0x00);
// code after

When executing the script, the following error message came up:

Fatal error: Call to undefined function imagecolorallocate() on line 81

I tried to reproduce and by typing it manually, wrote a script *with the exact same code.* It ran. As it was supposed to. I took the customer’s script and deleted *everything* before and after the culprit. Did the same with the duplicate script. I added the following at the start:


I saw, that the error message *actually* said:

Fatal error: Call to undefined function  imagecolorallocate() on line 81

There’s two spaces between `function` and `imagecolorallocate()`. One too many. One of them (the first) is `0x20`. The second was `0xA0`.

It turned out, the customer copy-pasted an example found somewhere on the internet from his/her browser into the script. The example code was automagically formatted using PHP’s built-in function [`highlight_string()`][php-hs]. That one turns any white space to a non-breaking whitespace (better known as ` `, or character code `0xA0`). **Which is considered legal input for identifiers by PHP.**

An example can be downloaded here: ghost.php.

Here’s what TortoiseSVN said when comparing both files:

This post has been purposefully tagged as a rant. I think the PHP team’s attempt to make the thing be usable by *anyone* without any mentionable programming background has gone too far. The intent was (guessing here) to enable German programmers the use of so called [Denglish][wiki] in their code without regret:

$breite = 320;         // width
$höhe = 200;           // height
if ($höhe > $breite)
   $hochkant = TRUE;   // landscape
else
   $hochkant = FALSE;

I have actually seen this kind of code. Reproducing it here makes me want to puke, actually.

**WTF.**

[php-hs]: http://php.net/manual/en/function.highlight-string.php
[wiki]: http://en.wikipedia.org/wiki/Denglisch