PHP String Concatenation - "$A $B" VS $A . " " . $B - Performance

PHP String concatenation - $a $b vs $a . . $b - performance

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

PHP is very inconsistent from version to version and build to build when it comes to performance, you have to test it for yourself.
What nees to be said is that it also depends on the type of $a, $b and $c, as you can see below.

When you use ", PHP parses the string to see if there are any variable/placeholders used inside of it, but if you use only ' PHP treats it as a simple string without any further processing. So generally ' should be faster. At least in theory. In practice you must test.


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 />';

?>

What's the fastest way of concatenating strings in PHP?

Code sample 1 won't work at all..

Syntax considerations set aside, Sample 1 should be trivially faster because it doesn't involve parsing a string (looking for variables).

But it's very, very trivial..

Performance overhead for PHP string concatenation?

Yes, this will increase performance. sprintf is an additonal function call, your string must be scanned for the %s which requires additional time.

The second option using the string concat operator (.) is faster, but the third alternative, just placing the string variable in a string is fastest due to other optimizations that PHP performs.

Anyway, while investigating how PHP deals with string concatination and how it performs is interesting you should never create sql queries like this, because it opens your code to SQL injections. apply mysql_real_escape() to your parameters first or use prepared statements.

php String Concatenation, Performance

No, there is no type of stringbuilder class in PHP, since strings are mutable.

That being said, there are different ways of building a string, depending on what you're doing.

echo, for example, will accept comma-separated tokens for output.

// This...
echo 'one', 'two';

// Is the same as this
echo 'one';
echo 'two';

What this means is that you can output a complex string without actually using concatenation, which would be slower

// This...
echo 'one', 'two';

// Is faster than this...
echo 'one' . 'two';

If you need to capture this output in a variable, you can do that with the output buffering functions.

Also, PHP's array performance is really good. If you want to do something like a comma-separated list of values, just use implode()

$values = array( 'one', 'two', 'three' );
$valueList = implode( ', ', $values );

Lastly, make sure you familiarize yourself with PHP's string type and it's different delimiters, and the implications of each.

Which is faster - $a.$b or $a$b

I would say option A

For basically the same reason you stated

concat php strings; using . operator or double quotes

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

PHP string concatenation using ,?

The . operator is the concatenation operator. Your first example only works because the echo 'function' (technically it's a language construct, but lets not split hairs) accepts more than one parameter, and will print each one.

So your first example is calling echo with more than one parameter, and they are all being printed, vs. the second example where all the strings are being concatentated and that one big string is being printed.

Why use sprintf function in PHP?

sprintf has all the formatting capabilities of the original printf which means you can do much more than just inserting variable values in strings.

For instance, specify number format (hex, decimal, octal), number of decimals, padding and more. Google for printf and you'll find plenty of examples. The wikipedia article on printf should get you started.

What is the best way to add two strings together?

You are always going to create a new string whe concatenating two or more strings together. This is not necessarily 'bad', but it can have performance implications in certain scenarios (like thousands/millions of concatenations in a tight loop). I am not a PHP guy, so I can't give you any advice on the semantics of the different ways of concatenating strings, but for a single string concatenation (or just a few), just make it readable. You are not going to see a performance hit from a low number of them.

How can I sort arrays and data in PHP?

Basic one dimensional arrays

$array = array(3, 5, 2, 8);

Applicable sort functions:

  • sort
  • rsort
  • asort
  • arsort
  • natsort
  • natcasesort
  • ksort
  • krsort

The difference between those is merely whether key-value associations are kept (the "a" functions), whether it sorts low-to-high or reverse ("r"), whether it sorts values or keys ("k") and how it compares values ("nat" vs. normal). See http://php.net/manual/en/array.sorting.php for an overview and links to further details.

Multi dimensional arrays, including arrays of objects

$array = array(
array('foo' => 'bar', 'baz' => 42),
array('foo' => ..., 'baz' => ...),
...
);

If you want to sort $array by the key 'foo' of each entry, you need a custom comparison function. The above sort and related functions work on simple values that they know how to compare and sort. PHP does not simply "know" what to do with a complex value like array('foo' => 'bar', 'baz' => 42) though; so you need to tell it.

To do that, you need to create a comparison function. That function takes two elements and must return 0 if these elements are considered equal, a value lower than 0 if the first value is lower and a value higher than 0 if the first value is higher. That's all that's needed:

function cmp(array $a, array $b) {
if ($a['foo'] < $b['foo']) {
return -1;
} else if ($a['foo'] > $b['foo']) {
return 1;
} else {
return 0;
}
}

Often, you will want to use an anonymous function as the callback. If you want to use a method or static method, see the other ways of specifying a callback in PHP.

You then use one of these functions:

  • usort
  • uasort
  • uksort

Again, they only differ in whether they keep key-value associations and sort by values or keys. Read their documentation for details.

Example usage:

usort($array, 'cmp');

usort will take two items from the array and call your cmp function with them. So cmp() will be called with $a as array('foo' => 'bar', 'baz' => 42) and $b as another array('foo' => ..., 'baz' => ...). The function then returns to usort which of the values was larger or whether they were equal. usort repeats this process passing different values for $a and $b until the array is sorted. The cmp function will be called many times, at least as many times as there are values in $array, with different combinations of values for $a and $b every time.

To get used to this idea, try this:

function cmp($a, $b) {
echo 'cmp called with $a:', PHP_EOL;
var_dump($a);
echo 'and $b:', PHP_EOL;
var_dump($b);
}

All you did was define a custom way to compare two items, that's all you need. That works with all sorts of values.

By the way, this works on any value, the values don't have to be complex arrays. If you have a custom comparison you want to do, you can do it on a simple array of numbers too.

sort sorts by reference and does not return anything useful!

Note that the array sorts in place, you do not need to assign the return value to anything. $array = sort($array) will replace the array with true, not with a sorted array. Just sort($array); works.

Custom numeric comparisons

If you want to sort by the baz key, which is numeric, all you need to do is:

function cmp(array $a, array $b) {
return $a['baz'] - $b['baz'];
}

Thanks to The PoWEr oF MATH this returns a value < 0, 0 or > 0 depending on whether $a is lower than, equal to or larger than $b.

Note that this won't work well for float values, since they'll be reduced to an int and lose precision. Use explicit -1, 0 and 1 return values instead.

Objects

If you have an array of objects, it works the same way:

function cmp($a, $b) {
return $a->baz - $b->baz;
}

Functions

You can do anything you need inside a comparison function, including calling functions:

function cmp(array $a, array $b) {
return someFunction($a['baz']) - someFunction($b['baz']);
}

Strings

A shortcut for the first string comparison version:

function cmp(array $a, array $b) {
return strcmp($a['foo'], $b['foo']);
}

strcmp does exactly what's expected of cmp here, it returns -1, 0 or 1.

Spaceship operator

PHP 7 introduced the spaceship operator, which unifies and simplifies equal/smaller/larger than comparisons across types:

function cmp(array $a, array $b) {
return $a['foo'] <=> $b['foo'];
}

Sorting by multiple fields

If you want to sort primarily by foo, but if foo is equal for two elements sort by baz:

function cmp(array $a, array $b) {
if (($cmp = strcmp($a['foo'], $b['foo'])) !== 0) {
return $cmp;
} else {
return $a['baz'] - $b['baz'];
}
}

For those familiar, this is equivalent to an SQL query with ORDER BY foo, baz.

Also see this very neat shorthand version and how to create such a comparison function dynamically for an arbitrary number of keys.

Sorting into a manual, static order

If you want to sort elements into a "manual order" like "foo", "bar", "baz":

function cmp(array $a, array $b) {
static $order = array('foo', 'bar', 'baz');
return array_search($a['foo'], $order) - array_search($b['foo'], $order);
}

For all the above, if you're using PHP 5.3 or higher (and you really should), use anonymous functions for shorter code and to avoid having another global function floating around:

usort($array, function (array $a, array $b) { return $a['baz'] - $b['baz']; });

That's how simple sorting a complex multi-dimensional array can be. Again, just think in terms of teaching PHP how to tell which of two items is "greater"; let PHP do the actual sorting.

Also for all of the above, to switch between ascending and descending order simply swap the $a and $b arguments around. E.g.:

return $a['baz'] - $b['baz']; // ascending
return $b['baz'] - $a['baz']; // descending

Sorting one array based on another

And then there's the peculiar array_multisort, which lets you sort one array based on another:

$array1 = array( 4,   6,   1);
$array2 = array('a', 'b', 'c');

The expected result here would be:

$array2 = array('c', 'a', 'b');  // the sorted order of $array1

Use array_multisort to get there:

array_multisort($array1, $array2);

As of PHP 5.5.0 you can use array_column to extract a column from a multi dimensional array and sort the array on that column:

array_multisort(array_column($array, 'foo'), SORT_DESC, $array);

You can also sort on more than one column each in either direction:

array_multisort(array_column($array, 'foo'), SORT_DESC,
array_column($array, 'bar'), SORT_ASC,
$array);

As of PHP 7.0.0 you can also extract properties from an array of objects.



If you have more common cases, feel free to edit this answer.



Related Topics



Leave a reply



Submit