PHP string concatenation is "$ a $ b" versus $ a. "". $ b - performance

Is there any difference in speed between, say:

$ newstring = "$ a and $ b came out to see $ c";

and

$ newstring = $ a. "as well as". $ b. "went out to see." $ S;

and if so, why?

+4
source share
7 answers

Depending on the version of PHP, it depends on how much the second is faster if you write it like this: $newstring = $a . ' and ' . $b . ' went out to see ' . $c; $newstring = $a . ' and ' . $b . ' went out to see ' . $c;

PHP is very incompatible from version to version and builds to build, when it comes to performance, you should check it out for yourself. What needs to be said is that it also depends on the types of $a , $b and $c , as you can see below.

When you use " , PHP parses the string to see if it has any variable / placeholders, but if you only use ' , PHP treats it as a simple string without further processing. Therefore, usually ' should be faster. At least , in theory. In practice, the test should .


Results (in seconds):

 a, b, c are integers: all inside " : 1.2370789051056 split up using " : 1.2362520694733 split up using ' : 1.2344131469727 a, b, c are strings: all inside " : 0.67671513557434 split up using " : 0.7719099521637 split up using ' : 0.78600907325745 <--- this is always the slowest in the group. PHP, 'nough said 

Using this code with Zend Server CE PHP 5.3:

 <?php echo 'a, b, c are integers:<br />'; $a = $b = $c = 123; $t = xdebug_time_index(); for($i = 1000000; $i > 0; $i--) $newstring = "$a and $b went out to see $c"; $t = xdebug_time_index() - $t; echo 'all inside " : ', $t, '<br />'; $t = xdebug_time_index(); for($i = 1000000; $i > 0; $i--) $newstring = $a . " and " . $b . " went out to see " . $c; $t = xdebug_time_index() - $t; echo 'split up using " : ', $t, '<br />'; $t = xdebug_time_index(); for($i = 1000000; $i > 0; $i--) $newstring = $a . ' and ' . $b . ' went out to see ' . $c; $t = xdebug_time_index() - $t; echo 'split up using \' : ', $t, '<br /><br />a, b, c are strings:<br />'; $a = $b = $c = '123'; $t = xdebug_time_index(); for($i = 1000000; $i > 0; $i--) $newstring = "$a and $b went out to see $c"; $t = xdebug_time_index() - $t; echo 'all inside " : ', $t, '<br />'; $t = xdebug_time_index(); for($i = 1000000; $i > 0; $i--) $newstring = $a . " and " . $b . " went out to see " . $c; $t = xdebug_time_index() - $t; echo 'split up using " : ', $t, '<br />'; $t = xdebug_time_index(); for($i = 1000000; $i > 0; $i--) $newstring = $a . ' and ' . $b . ' went out to see ' . $c; $t = xdebug_time_index() - $t; echo 'split up using \' : ', $t, '<br />'; ?> 
+16
source

There will probably be a difference in speed, as these are two different syntaxes. What you need to ask is the difference is important. In this case, no, I don’t think you need to worry. The difference would be too slight.

I would recommend you to do what is of great benefit to you visually. " $a and $b went out to see $c " can be a little confusing when you look at it. If you want to go this route, I suggest braces around your variables: " {$a} and {$b} went out to see {$c} ".

+6
source

I did a quick test, and as others said, the results were very inconsistent. I did not notice a performance increase using single quotes instead of double quotes. I guess it all comes down to preference.

You might want to stick to one type of quote for your coding style, and if you do, select double quotes. The replacement feature comes in handy more often than you think.

I put control code in github .

+2
source

If you are concerned about the string concatenation speed at this level, you are using the wrong language. Compile the application in C for this use case and name it in your PHP script if this is really a bottleneck.

+2
source

Yes, there is a difference, however, a very slight difference between

 $newstring = "$a and $b went out to see $c"; 

and

 $newstring = $a . " and " . $b . " went out to see " . $c; 

If you used:

 $newstring = $a . ' and ' . $b . ' went out to see ' . $c; 

The difference would be slightly larger (but probably still not significant), the reason is that if I remember correctly (maybe I'm wrong), PHP checks and analyzes the contents in double quotes for variables and special characters (\ t, \ n etc.), and when using single quotes, it does not analyze variables or special characters, so there may be a slight increase in speed.

+1
source

Why don't you check it out and compare the difference? Numbers don't lie, if you find that one works better than the other, then you should ask why.

+1
source

no difference, period .;)

-2
source

All Articles