views:

1022

answers:

5

(assume php5) consider

<?php
$foo = 'some words';

//case 1
print "these are $foo";

//case 2
print "these are {$foo}";

//case 3
print 'these are ' . $foo;

Is there much of a difference between 1 and 2?

If not, what about between 1/2 and 3?

+2  A: 

My gut tells me that concatenation would be faster, if only because it doesn't require parsing of the contents of the string to find and sub in the variable value. If there is a real speed difference, though, I've never noticed it in my own code.

As for 1 and 2, the only time I'll use the curly braces for string interpolation is when they are required, which, as far as I know, is when you're accessing an array element or going more than one deep in object syntax, like so:

$dog->innards->spleen
Brian Warshaw
+11  A: 

Well, as with all "What might be faster in real life" questions, you can't beat a real life test.

function timeFunc($function, $runs)
{
  $times = array();

  for ($i = 0; $i < $runs; $i++)
  {
    $time = microtime();
    call_user_func($function);
    $times[$i] = microtime() - $time;
  }

  return array_sum($times) / $runs;
}

function Method1()
{ 
  $foo = 'some words';
  for ($i = 0; $i < 10000; $i++)
    $t = "these are $foo";
}

function Method2()
{
  $foo = 'some words';
  for ($i = 0; $i < 10000; $i++)
    $t = "these are {$foo}";
}

function Method3()
 {
  $foo = 'some words';
  for ($i = 0; $i < 10000; $i++)
    $t = "these are " . $foo;
}

print timeFunc('Method1', 10) . "\n";
print timeFunc('Method2', 10) . "\n";
print timeFunc('Method3', 10) . "\n";

Give it a few runs to page everything in, then...

0.0035568

0.0035388

0.0025394

So, as expected, the interpolation are virtually identical (noise level differences, probably due to the extra characters the interpolation engine needs to handle). Straight up concatenation is about 66% of the speed, which is no great shock. The interpolation parser will look, find nothing to do, then finish with a simple internal string concat. Even if the concat were expensive, the interpolator will still have to do it, after all the work to parse out the variable and trim/copy up the original string.

Adam Wright
+8  A: 

@Adam's test used

"these are " . $foo

note that the following is even faster:

'these are ' . $foo;

this is due to the fact, that a double quoted "string" gets evaluated, where a single quoted 'string' is just taken as is...

Pierre Spring
I've just done some quick testing and there's not much of a saving between these two - certainly nowhere near as much as changing interpolation to concatenation - but single quotes are faster.
Colonel Sponsz
+1  A: 

Don't get too caught up on trying to optimize string operations in PHP. Concatenation vs. interpolation is meaningless (in real world performance) if your database queries are poorly written or you aren't using any kind of caching scheme. Write your string operations in such a way that debugging your code later will be easy, the performance differences are negligible.

@uberfuzzy Assuming this is just a question about language minutia, I suppose it's fine. I'm just trying to add to the conversation that comparing performance between single-quote, double-quote and heredoc in real world applications in meaningless when compared to the real performance sinks, such as poor database queries.

jakemcgraw
A: 

@gomercobs who said anything about databases? i was just asking a question that has been rolling around in my head since i started using php years ago.

Uberfuzzy