Tip of the Day (2/9/11) - Parsing Variables in strings

[thread=732239]Previous tip - Datatypes and Casting[/thread]

A few days ago someone posted code looking like this


$a = "$b";

This takes advantage of the fact that a variable that appears in double quotes will be “parsed” into the assignment. So in the above whatever the value of b is will be converted to a string and given to a.

Using the parsing engine for this is inefficient. The correct assignment is


$a = $b

or


$a = (string) $b

But the parsing engine is still useful for things like


$a = "The quick brown $b jumped over the lazy $c";

What if you don’t want parsing to occur? Use single quotes


$a = 'The price $anything is $1.00';

Things get a little complicated with arrays and objects. To properly parse them in use braces like so.


$a = "The {$animal->speed} {$animal->color} {$animal->type} jumped over the {$sleepy['speed']} {$sleepy['type']}";

Do not use $sleepy[type] (note the absence of ’ characters). This relies on the constant type to be undeclared (returning the string ‘type’) and will break if someone defines type.

That’s all for now. Tommorrow I’ll look at HEREDOCs and its new cousin (as of PHP 5.3) NOWDOCs.

Keep in mind that single quotes and concatenation is faster. Which has better readability is debatable, but I find arrays in braces in double quotes harder to read than concatenation.

Hear hear.

I prefer to use commas in echo as well, instead of concatenation. Apparently that’s even faster.

Does anyone have a definite answer to this frequently debated question - is it faster to use single quotes because parsing engine is not involved? Some people are saying that if you don’t have variables inside a string then you should use single quotes because php will try to parse the string when it sees the double quotes. It sounds like logically this is true, but I also heard arguments that it does not really make any difference in performance.

I am looking for some type of confirmation of one or the other theory.
I personally always use single quotes and almost never use this feature of php to add variables to a string.

At least on the IDE I use the color context picks up on the variables in braces and makes them really pop out from the surrounding string. Where that not the case I could see an argument for using the concatenation operator.

Untrue.

Here’s what Gwynne Raskind from PHP team has to say about it (look at point #2, or even better read the whole thing, which debunks quite a few myths re “optimisation”):

http://groups.google.com/group/make-the-web-faster/browse_thread/thread/ddfbe82dd80408cc

The speed of ’ versus " wasn’t something I wanted to bring up because if someone is writing code like this


$a = "$b";

It’s a little early to be asking them to worry about speed issues. Whichever way the ball bounces I don’t think it’s going to affect the small sites a novice programmer will be called on to work on.

My concern was to introduce the syntaxes. Also, single quotes are justified when you don’t want $ signs to be parsed, among other concerns.

Interesting, last time I read up on it, this was the highly referenced article: http://blog.golemon.com/2006/06/how-long-is-piece-of-string.html Although it is from 06 so perhaps I have some catching up to do.