views:

180

answers:

7

Is it better to concatenate a variable (say, $name) into an existing string (say, $string) like this:

$string='Hi, my name is '.$name

or like this:

$string="Hi, my name is $name";

or like this:

$string=sprintf("Hi, my name is %s",$name);

in terms of processor time/efficiency.

A: 

It doesn't matter from syntax perspective. Both variants are correct. Use what you feel more comfortable.

Personally, I feel better when using the $string="Hi, my name is $name", because you don't need to mess with quotes. Just image the complex SQL query with, let's say, 10 variables...

Andrejs Cainikovs
+1  A: 

I generally feel that using string interpolation ("Hi, my name is $name") is better from a legibility standpoint.

Flavius Stef
+5  A: 

Everyone who did the test concluded that using single quotes is marginally better performance wise. In the end single quotes result in just a concatenation while double quotes forces the interpreter to parse the complete string for variables.

However the added load in doing that is so small for the last versions of PHP that most of the time the conclusion is that it doesn't really matter.

So for the performance people: use single quotes. For the "i like my code readable"-people: double quotes are a lot better for the legibility, as Flavius Stef already pointed out.

Edit: One thing though - If you are going to use a a single dollar in your string without a variable, use single quotes for sure! (http://www.weberdev.com/get_example-3750.html points out that it will take 4 times longer to parse those strings)

Blizz
"everyone who did the test..." data source?
ina
http://classyllama.com/development/php/php-single-vs-double-quotes/http://www.procata.com/blog/archives/2005/03/08/microbenchmarks-of-single-and-double-qouting/Here are 2. google can give you plenty more.
Blizz
I think test is even not required. It is just a matter of number of processor instructions. The less you have (that can be easily guessable in this case), the faster you are.
snowflake
The parsing is just done once. There is no additional parsing for double quoted strings.
Gumbo
Of course the parsing is done just once. But the the steps within the parsing-proccess are meant. With double quotes the parser has to add the step of variable-interpolation to its parsing-chain.
faileN
A: 

PHP is pretty slow:

http://ocw.mit.edu/courses/electrical-engineering-and-computer-science/6-088-introduction-to-c-memory-management-and-c-object-oriented-programming-january-iap-2010/lecture-notes/MIT6_088IAP10_lec01.pdf

Slide #3

So don't worry too much about little optimizations like these.

Focus more on using APC to cache your code into byte code though. You'll see big speed gains for the project.

Homer6
A: 

$string='Hi, my name is '.$name --- this is the best!!! in the sense of php, php and html combination!

or like this:

$string="Hi, my name is $name"; ---- this is the old one!

or like this:

$string=sprintf("Hi, my name is %s",$name); --- this create programmer they coming from Visual Basic and other Client Programming languages!

i hope i cant help

stooni1395

mstooni1395
A: 

Personally, if it's just a normal variable, or even a class property, I'd write it like this:

$newVarA = "This is some text with a $variable";
$newVarB = "This is some more text, written in $settings->language";

However, if I'm using array values then I'll concatenate with single quotes.

$newVarC = 'This is some text from a ' . $text['random'] . ' array';

Hope this makes sense. It's all about finding convention and sticking to it.

Martin Bean
Your array example works just fine without the concatenation. `... from a {$text['random']} array`, or `... from a $text[random] array`. You'd only *have* to use the {} method if you're referencing a multi-dimensional array, as PHP's parser isn't greedy. `$array[x][y]` will get parsed as `<contents of $array[x]><plain text [y]>`.
Marc B
Yes. But I find using braces in strings just gets a bit ugly to me. Like I say, it's just an example of how I personally concatenate strings with variables.
Martin Bean
+4  A: 

The difference between single and double quotes in PHP is that double quotes are "intelligent" in that they will parse for variables when being read, while single quotes are "dumb" and will not try to parse any character in the string.

These result in some minor differences in what characters you can use; basically, the only character you need to escape when using single quotes is a single quote itself:

'\''

While if you use double quotes you have to escape other characters:

"\$"

But it also allows for some nifty things like adding a new-line to the end:

"my string\n"

With single quotes you would have to do a concatenation:

'my string' . chr(10)
'my string' . "\n"

Generally, single quotes are faster because they are "dumb".

However, normally one should not really worry about these issues, that is called Premature optimization, and should be avoided.

A couple of words about optimization: generally one should first write the program the way it should work, and then find the biggest bottlenecks and fix those particular ones. If string speed really is an issue for you in PHP, you might want to consider switching to another language.

Regarding speed: you probably want to focus more on memory usage than on CPU time. In these cases the CPU time could be considered pretty constant. CPU time is more relevant when writing algorithms that will iterate many times.

Regarding concatenations: the more you concatenate strings using the dot-operator, the more memory you will be using.

Consider this:

$str1 = 'asdf';
$str2 = 'qwer';

// this will result in more memory being allocated for temporary storage
echo $str1 . $str2;

// this will not allocate as much memory as the previous example
echo $str1;
echo $str2;
thnee