- Kim Bach . Org - https://www.kimbach.org -

PHP blues – drawing a blank

Software development can be extremely frustrating, and it might be true that the computer only does what you tell it to do, but these days a lot of people, besides yourself, are telling the computer what to do, a fact that complicates matters “somewhat” (understatement of the year).

The following experience documents a case, where I was sure that I wasn’t doing anything wrong, and you could argue that I didn’t, I just didn’t know any better, but thanks to the community I solved the problem.

The setting

I’ve recently begun doing some PHP programming, and I like it a lot, mostly because of all the wonderful community. it’s my gut feeling that I find answers to my questions much faster when I have PHP related questions, compared to .NET questions.

So I was experimenting with generating some XML code from PHP, but when I gave the URL to the W3C validator, it told me that the XML wasn’t well-formed, and that it was the header that was the problem.

I couldn’t see anything wrong, until I took a closer look. There was a mysterious space prefixed the output when I did a View/Source, a space that I for sure knew I didn’t echo. Hmm…

I tried several things, including flushing the output, but nothing helped.

Then it dawned on me: it’s very likely that someone else has had this problem, and if they did, for sure they would have the generosity to share it, so Google to rescue.

I googled: problem php output leading blank [1], and hit number two yielded this article:

Script for removing blank spaces before and after PHP tags « memento [2]

The solution

It turns out that PHP is quite sensitive if you, by accident, leave prefixed or sufficed blank spaces outside the starting and ending php tags.

So I went through my PHP files, luckily I only have two, made sure there were no leading or trailing blanks og lines, and I get well-formed XML now.

This is a rather annoying bug in, or should I say feature of, PHP, and it exists in both PHP4 and PHP5.

Once again the community came through, without it I’d still be drawing blanks.

And I love the drawing [3] on the memento site, it doesn’t get geekier, and I think I’ll remember to strip leading and trailing blanks now.

I’m still looking for a modern development environment for PHP development (e.g. sourcecode debugging). PHPEclipse [4], a plug-in for the Eclipse [5] development environment, should provide a relatively modern development environment, even though it’s not up to the standards set by Microsoft VisualStudio, that I’m so accustomed to, Microsoft has spoiled me.