The Case of the Non-missing Newline

05Apr12

It has been really great working on Computer Science Circles, the project that I’m working on at Waterloo which is a website to teach programming for beginners. Although it teaches Python, because it’s built over top of WordPress, I have also written a lot of JavaScript and PHP. All of these are fine languages, and if I were pressed to explain the difference, I’d say:

  • you can do in principle do anything you want with any language,
  • JavaScript is designed for manipulating the web browser,
  • PHP is designed for producing html pages to be sent to the browser,
  • Python is designed to be easy to understand, without as much of a specialty.

Probably 80% of the code I’ve written for the site is in PHP. The special power of PHP is to put little function calls inside fixed text, which is useful for generating web pages. Here’s an example, which you can imagine as part of the text of an .html file:

<div>The time is <?php echo time(); ?> and my lucky number is <?php 3+4; ?>.</div>

This will print out the current time as part of the .html page whenever it is accessed.

I found that there was a jam session for PHP taking place in my area tonight. Pretty random sounding, but it’s always good to find things out from people with more experience. I asked some questions about sandboxing and grid displays and got some good suggestions for further investigation. Apparently “frameworks” are all the rage but I am not caught up to speed on that yet. Fast forward to the one snippet of code which someone wrote on the whiteboard,

<?php
echo "Hello, World!";

Pretty trivial, but very useful! I never really thought to notice, but the end of the file makes a big difference. You can include a ?> or leave it out like the second example shows.

A while ago, a weird bug arose in the CS Circles site: information being to JavaScript was corrupted in a minor way. Some messages being returned would randomly start with \n newline characters. I did a workaround in JavaScript and promptly forgot about the issue.

So tonight, without asking anyone, the cause of the bug was revealed! Hello World solved all my problems. The bug happens when you

  1. leave the ?> at the end of the file
  2. put a newline after the ?>, which causes the newline to be printed out since it’s outside of <?php … ?>, and
  3. send data to JavaScript rather than just making an .html page, since .html doesn’t generally care about where your newlines are.

In retrospect I knew all of these facts but it’s pretty subtle. I can’t think of any other programming I’ve done where I would even imagine that a newline at the end of file could make a difference.

It’s satisfying to finally fix the bug and remove the old hack… like a stupidity epiphany! If you want a script to detect this problem automatically, check out the page “God forbid you have extra space at the end of your PHP file!“. Guess I am not alone in my epiphany.

Advertisements


2 Responses to “The Case of the Non-missing Newline”

  1. Uh oh, with your pro-PHP stance you’re very unfashionable:
    http://me.veekun.com/blog/2012/04/09/php-a-fractal-of-bad-design/

    Joel Spolsky is slightly more fogiving:
    http://www.codinghorror.com/blog/2008/05/php-sucks-but-it-doesnt-matter.html

    Here’s a ranking of the most popular web frameworks. PHP has the most popular ones, but Python’s Django is pretty high up.
    http://hotframeworks.com/

    I’ve dabbled a bit in Django too and I like it a lot:
    http://timbenke.de/?p=480

    Your code above could “nearly” be done. The difference being that you are not allowed to used arguments in the functions you’re calling in Django html templates. The reasoning behind it is that you should separate UI logic from the html to have looser coupling…

  2. I especially like the comment that PHP comes with the kitchen sink, but it’s from Canada and both faucets are labeled C. Thought it was anti-Canadian until I checked out the link.


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s


%d bloggers like this: