Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

concat php strings; using . operator or double quotes

Tags:

string

php

Edit - My question is not stricktly limited to preformance, I would also like to know the pitfalls of each and if there is a condition where one should be used over the other.

Which is better to use to concat a string in PHP?

Option A: Use the . operator to concat the strings

$string1 = "hello ";
$string2 = $string1 . "world !";

Option B: Use double quotes

$string1 = "hello ";
$string2 = "$string1 world !";

I realize that both will in fact do the same thing, and in my personal development I prefer to use the . operator. My question only arises because i've read that the . operator forces php to re-concatenate with each new string, so in the example:

$string1 = "hello ";
$string2 = "world";
$string3 = $string1.$string2." !";

would actually run slower than

$stirng1 = "hello";
$string2 = "world";
$string3 = "$string1 $string2 !";

Reference: PHP Language Operators > Strings

like image 680
rlemon Avatar asked Dec 28 '22 16:12

rlemon


2 Answers

I think before you start worrying about it, you need to see if it is even worth thinking about. I did think about it, and wrote the following tiny script and ran it to see what the benchmarks were like.

For each loop, I made 100,000 passes. Now I didn't print my strings anywhere so if the PHP optimizer takes all of my work away because of that, then I apologize. However looking at these results, you are looking at a difference of about 0.00001 second for each.

Before you optimize for anything other than readability, use a profiler and see where your hotspots are. If you run tens of millions of concatenations, then you may have an argument. But with 1000, you are still talking about a difference of 0.01 seconds. I'm sure you could save more than 0.01 seconds just by optimizing SQL queries and the like.

My evidence is below....

Here's what I ran:

<?php
for($l = 0; $l < 5; $l++)
  {
    echo "Pass " .$l. ": \n";
    $starta = microtime(1);
    for( $i = 0; $i < 100000; $i++)
      {
    $a = md5(rand());
    $b = md5(rand());
    $c = "$a $b".' Hello';
      }
    $enda = microtime(1);

    $startb = microtime(1);
    for( $i = 0; $i < 100000; $i++)
      {
    $a = md5(rand());
    $b = md5(rand());
    $c = $a . ' ' . $b . ' Hello';
      }
    $endb = microtime(1);


    echo "\tFirst method: " . ($enda - $starta) . "\n";
    echo "\tSecond method: " . ($endb - $startb) . "\n";
  }

Here are the results:

Pass 0: 
    First method: 1.3060460090637
    Second method: 1.3552670478821
Pass 1: 
    First method: 1.2648279666901
    Second method: 1.2579910755157
Pass 2: 
    First method: 1.2534148693085
    Second method: 1.2467019557953
Pass 3: 
    First method: 1.2516458034515
    Second method: 1.2479140758514
Pass 4: 
    First method: 1.2541329860687
    Second method: 1.2839770317078
like image 121
KyleWpppd Avatar answered Dec 30 '22 10:12

KyleWpppd


Concatenation is almost always faster than interpolation, but the difference is rarely significant enough to warrant caring. That said, I prefer concatenation because it allows easier editing when (for example) you want to change a string to a method or function call. I.e., from:

$s1 = 'this ' . $thing . ' with a thing';

To:

$s1 = 'this ' . blarg($thing) . ' with a thing';

Edit: When I say, "Concatenation is almost always faster than interpolation," what I mean is, I have actually benchmarked many various forms of it, and I'm not just guessing, or reiterating somebody else's post. It's easy to do, try it.

like image 26
Alex Howansky Avatar answered Dec 30 '22 09:12

Alex Howansky