Blogs Bookmarks Computere og Internet Hackers Kim Blog (English) Open Source Technology

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, and hit number two yielded this article:

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

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 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, a plug-in for the Eclipse 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.

3 replies on “PHP blues – drawing a blank”

Well, it’s a feature, not a bug (in a way).

It’s a function of PHP’s origins. In the bad old days, you’d just write HTML and sprinkle it with php-tags and variables.

Files accessed from PHP are processed sequentially, so PHP does not do any work until it sees a would invoke PHP which would throw an error and die.

I remember spending countless hours chasing down syntax errors in PHP. A missing } could take ages to find, because the error would not get thrown before the end of the PHP tag, and if it was a long file, one would sit there counting brackets for a long time.

Thankfully, we have more advanced tools today. And perhaps I’ve gotten better at programming, too 😉

hi Kim,

you are right. PHP has a big and active community support and most of the times, you will find the solution just by googling it. And just one tip that you might find helpful is that you don’t necessarily need closing PHP tag i.e ?> . Its optional. However, I would recomment to always use it as it makes the code more readable.

Leave a Reply

Your email address will not be published. Required fields are marked *