About the Author

Chris Shiflett

Hi, I’m Chris, a web craftsman making things like Mapalong & Brooklyn Beta with my friends at Analog.


Richard Davey Has a Blog

I just noticed that another prominent member of the PHP community has started a blog. Richard Davey has been answering questions on various PHP mailing lists and forums for years, and now he has his own blog. Are you subscribed?

Note: The list of blogs I read is much more complete than the feed. Anyone know if this is a known del.icio.us bug?

One of his first entries is a response to a rather poor list of PHP tips. Richard counters many of the supposed performance tips that focus on trivial details such as single quotes versus double quotes. He also mentions the importance of readability. I think this is a point that is often lost, especially for developers who seek to impress others with the complexity of their code. Personally, I feel successful when my solution to a problem is unimpressive and painfully simple.

One thing that surprises me is that Richard seems to agree that concatenation is more readable than interpolation. Personally, I prefer interpolation:

<?php  

$name 
'Chris'
$location 'New York'

echo 
"My name is $name, and I live in $location."

?>

I think concatenation is less clear:

<?php  

$name 
'Chris'
$location 'New York'

echo 
'My name is ' $name ', and I live in ' $location '.'

?>

You could argue that concatenation is clearer to PHP, but I try to focus on what's clearer to the developer. Which do you prefer?

Update: Richard says he usually prefers interpolation. I guess we agree on all points. :-)

About this post

Richard Davey Has a Blog was posted on Mon, 07 Nov 2005 at 20:13:29 GMT. Follow me on Twitter.

17 comments

1.Nando Vieira said:

I have one rule when using variables inside strings. I always put as {$variable_name}.

echo "My name is {$name}, and I live in {$location}";

Now that I'm playing with Python, I'm using lots of sprintf/printf.

printf('My name is %s, and I live in %s', $name, $location);

That's it!

Mon, 07 Nov 2005 at 20:35:39 GMT Link


2.Richard Davey said:

Opps, I guess I should have actually commented on the interpolation vs. concats tip, rather that skip it over! As it does indeed sound that by not picking up on it I was agreeing with it. Even though that isn't actually the case, as I very rarely concat strings, prefering nearly always to interpolate them. But it does depend on the situation, I'm not sure there is a clear cut black/white for this. I wonder if the pear guidelines have anything to say on it? because some packages do one, and others do the other!

Mon, 07 Nov 2005 at 21:33:37 GMT Link


3.JD said:

I prefer concatenation. This way variables stand out in my PHP code and it makes it more readable, IMHO.

Mon, 07 Nov 2005 at 21:36:54 GMT Link


4.mike said:

I prefer concat because PHP syntax coloring is able to pickout the variables. When scanning a document my eyes see the colored variable faster than they would see the $ sign. It's usually easier for me to read. However, in your basic example it reduces clutter - but real-world apps are bit more complicated.

Mon, 07 Nov 2005 at 21:43:17 GMT Link


5.Rob Allen said:

Using ZDE, php variables are picked out in interpolation too.

I personally tend to alternate. I always concatenate if I'm using an array element as I dislike using {}

Mon, 07 Nov 2005 at 21:47:11 GMT Link


6.mats said:

Concatenation! Explicitly requesting string concatenation helps readability (imho) and removes the problem mentioned about {} etc.

Mon, 07 Nov 2005 at 22:31:12 GMT Link


7.Ben Ramsey said:

I tend to prefer interpolation now for the same reasons you mention, though I started out with concatenation. However, I side with Nando Vieira in that I like to write it with curly braces, as I find it more readable:

echo "My name is {$name}, and I live in {$location}.";

Oh, I had thought you quit reading my blog since it wasn't showing up in your sidebar there. It's nice to see that I'm still in your del.icio.us blogroll. ;-)

Mon, 07 Nov 2005 at 22:53:07 GMT Link


8.Jonas said:

I prefer concat because I use single quoatas all the time, this way I don't need to think about what's within the quotas, and I also don't need to think about if it's an attribute or a function or an array...

I prefer to writhe more in general :-), readability is most important, because I think you'll spend most time for planing and testing, and both tasks are easyier if you have more readable code.

Tue, 08 Nov 2005 at 10:22:16 GMT Link


9.Matthew said:

I prefer concatentation. Many variables I use in more complex projects are array elements or object properties -- often more than a single level deep. Sure, I could use curly braces and do interpolation, but concatenation makes it easer to scan through the document for variables, and more likely that I'll get syntax and quoting correct. Additionally, if I adopt the same strategy for all strings, then as I look through my code, I can look for the same patterns (instead of looking in some cases for interpolation, other times for concatenation). In my case, it's an issue of being consistent in coding practice.

Tue, 08 Nov 2005 at 14:58:57 GMT Link


10.Dr. said:

<dr.snuggles>

Tue, 08 Nov 2005 at 19:43:27 GMT Link


11.Norbert Mocsnik said:

Definitely concatenation and proper use of single/double quotes.

Wed, 09 Nov 2005 at 13:53:00 GMT Link


12.Ren said:

Why use concatenation or interpolation in this case?

echo takes a list of things.

echo 'My name is ', $name, ', and I live in ', $location, '.';

:)

Thu, 10 Nov 2005 at 00:01:36 GMT Link


13.Kevin said:

I like concatenation because my coding software color codes the variables differently than the strings and it makes it much easier for me to seek and destroy when needed :)

Fri, 11 Nov 2005 at 04:20:30 GMT Link


14.Mary said:

<p>I too prefer concatenation, so my text editor’s highlighted source can be more useful; it does hideous things with {$var} and {$array['var']}. However, I will use whatever appears the cleanest/easiest to read for the particular string.</p>

<p>By the way, I love your blog.</p>

Sun, 13 Nov 2005 at 10:05:24 GMT Link


15.Jay Blanchard said:

I prefer concatenation as well and use it to be consistant. In SQL statements, where single quotes are often required to surround values it is a requirement;

<pre>

$sql = "SELECT foo ";

$sql .= "FROM bars ";

$sql .= "WHERE foobar = '". aHeavilyValidated($_POST['glorp']) ."' ";

</pre>

This makes the variables stand out and keeps the SQL syntax proper. I just continue that for most of my other coding.

Fri, 02 Dec 2005 at 16:05:14 GMT Link


16.Chris Shiflett said:

Mary, Thanks for the kind words. :-)

Most people seem to prefer concatenation, which surprises me a bit. However, I do understand that it better satisfies the pendantic hunger within us.

I like Ben's suggestion to always use curly braces, even when they're not necessary. I've seen others who follow that convention, and it seems like a pretty good approach.

Thu, 08 Dec 2005 at 06:30:41 GMT Link


17.Bryan Smith said:

Just a quick point. Concatenation will actually perform better than Interpolation. It may not be noticable in small scripts, but larger and more complex script will show a slowdown when using Interpolation.

Wed, 01 Feb 2006 at 18:06:09 GMT Link


Hello! What’s your name?

Want to comment? Please connect with Twitter to join the discussion.


Work and Books

Analog Essential PHP Security HTTP Developer's Handbook