Posts Tagged ‘PHP’

Forcing files to be interpreted as PHP with Apache

Saturday, June 20th, 2009

I’ve been writing a couple of web services lately which returns XML data. If I’m on a deadline and just have to get it up and running, I usually don’t care for small details, but a hobby project is another matter. When I have the time, I like to sit and tweak the code to get the response looking as clean as possible, giving meaningful error messages, logging errors to files etc.

Let’s say I’m writing an application that retrieves a list of notes from a web service. The URL for retrieving the XML data would most often look something like:

http://whatever.webservice.com/feeds/latest.php?since=timestamp

Which is fine, except one little aspect: it doesn’t tell me if I am getting data back as XML, JSON, CSV or in some other format. I would much rather prefer to have this specified in the URL. Examples of good, clean URLs would be any of the following:

http://whatever.webservice.com/feeds/xml/latest.php?since=timestamp
http://whatever.webservice.com/feeds/latest.php?format=xml&since=timestamp
http://whatever.webservice.com/feeds/latest.xml?since=timestamp

I personally think the last one is the prettiest. There are reasons why the two first might be “better” from a code and performance perspective though:

  • The subdirectory method could handle outputting of the data in that format and only that format, not needing any if-checks.
  • The query parameter method could handle a lot of different formats in the same file, and would simply call the correct function to retrieve the data in the correct format.

The last one was what I went for in the end. The problem with that is making Apache understand that this “XML” file is really a PHP file. I solved this easily with this simple  .htaccess file, which forces every XML file within the directory you place it in to be run through PHP:

<Files *.xml>
ForceType application/x-httpd-php
</Files>

Sending the correct headers is also a bonus:

<?php
header('Content-Type: text/xml; charset=utf-8');

Worth the effort? You tell me ;-)

String literals and lazy programmers

Friday, June 19th, 2009

A lot of programmers are passionate about what they feel is the “right” way to do things. A common topic of discussion is code standards, especially questions such as “Do brackets go on a separate line, or at the end of a statement”. It’s understandable, because I see pros and cons in both. On other topics, I have a hard time understanding both sides – such as whether to use tabs or spaces for indentation (I only ever use tabs). What we can all agree on however is what I started with: programmers are passionate about “the right way” to do things.

“String literals’

Heh, that was clever. See how I used both ” and ‘ in the heading there?

PHP is a funny language in a lot of ways. One of the things it does, along with some other languages such as Javascript, is allow you to specify strings using both double and single quotation marks (is that the right name for them?). In PHP, I generally just use what is most convenient at the time. There are differences between them (in PHP), which I’m sure most people are aware of already (double allows you to use variables directly in the string, using single you have to hop out and concatenate the variable with the . character. Some programmers apparently see this as an enormous amount of effort, and just use double quotes instead. Lazyness.

I generally find it easier to read code where variables are concatenated. If you want to rewrite the code to use values from an array instead of different variables, you either end up with the even uglier {$aArray['key']} style, or you end up rewriting the code to use concatenation anyway. Why not just do it “the right way” the first time you write the code?

The horror!

Even more horrible is using the single quotation marks for (X)HTML attributes. Most of the time people get this one right, but once in a while I stumble across pages that use the single quotation mark for HTML attributes – horrible stuff, even if it is “legal”. I was looking through the HTML for my blog today and fell off my chair when I saw this:

<ul class='xoxo blogroll'>

After peeking through the WordPress code, I discovered that this was a someone obviously does not share my hatred for using single quotation marks in HTML ;-) .

Really though:

$output .= "$title_before$title_li$title_after\n\t
    \n"; // Could easily be written: $output .= "$title_before$title_li$title_after\n\t
      \n"; // Or... $output .= $title_before . $title_li . $title_after . "\n\t" . '
        ' . "\n";

Personally, I prefer the last one, as I find it much easier to read. Plenty more grep’ing later, I think I’ve translated most of the occurrences (at least on the front page).

What are other peoples views on string literals? Am I just being a complete code nazi here?