All String Concatenations in PHP

In PHP, string concatenation is a common operation used to combine multiple strings into a single one. There are no less than 4 ways to combine several strings into one. Each have it own usage, adapted to each context, and it is a good coding skill to know them all. In this blog post, we’ll explore all the string concatenation in PHP, including the dot operator, the short assignment operator .=, string interpolation, and the implode() function.

Dot Operator .

The dot operator . is the most straightforward way to concatenate strings in PHP. It simply joins two strings together, creating a new string containing the combined text. The . operator may very well be chained to combine several values together.

<?php

$string1 = "Hello";
$string2 = "world";
$string  = $string1 . " " . $string2; 
// Hello world

?>

Short Assignment Operator .=

The short assignment operator .= is a special case of the dot operator. It appends the right operand to the left one.

<?php

$string = "Hello";
$moreString = " world";
$existingString .= $moreString; 
// Hello world

?>

Note that the right operand might also be a concatenation itself: in that case, the result of the whole concatenation on the right is appended to the variable.

There is no operator to prepend strings: that is, to add a string at the beginning of the left operand. The workaround is to use the concatenation operator, and repeat the variable.

<?php

$moreString = "Hello";
$string = " world";
$string = $moreString . $string; 
// Hello world

?>

String Interpolation

String interpolation allows embedding variables directly within double-quoted strings. This feature simplifies string concatenation by automatically evaluating variables within the string. This applies to variables, properties and arrays (although, up to one level only).

<?php

$moreString = "Hello";
$string = "$moreString world"; 
// Hello world

?>

String interpolation helps reduce the amount of dot operator usage, by mixing variables and literal strings. On the other hand, some structures become quickly difficult to read.

implode() Function

The implode() function is primarily used to join all the array elements into a single string, by concatenation. It also features a glue, which is a string that is automatically added between the elements. Use the empty string when a simple concatenation is needed: otherwise, use a separator.

<?php

$words = array("Hello", "world");
$string = implode(" ", $words); 
// Hello world

?>

implode() is really useful when an arbitrary number of strings are collected in an array, to be later turned into a string. Instead of doing as many concatenation as there are elements in the array (minus one), implode executes the operation in one take, and saves memory and execution cycles.

Using str_replace() for Simple Template Systems

One lesser known approach to concatenation is to set up a simple Template system with str_replace(). With such system, a template string is build with placeholders, that are replaced later by a dynamic value. In simple template systems, you may use placeholders in a template string and replace them with dynamic content. The str_replace() function can be handy for this purpose. str_replace() is able to execute multiple replacements at the same time, making the operation quite efficient.

<?php

$template = "<hello>, <name>!";
$placeholders = ['<hello>', '<name>'];
$english = ['hello', 'world'];
$french = ['bonjour', 'le monde'];

$message_en = str_replace($placeholders, $english, $template);
// hello world
$message_fr = str_replace($placeholders, $french, $template);
// bonjour le monde

?>

PHP has several other functions that can be used in this context : preg_replace(), for replacement based on regex; preg_replace_callback(), for replacement with complex calculations; strtr() for single character replacements.

This approach has been refined over the years into full blown templating engines, such as Twig, blade, plates, etc. They are much more powerful, but also more resource hungry. Sometimes, one only need only a simple concatenation.

Concatenating all strings

PHP offers several methods for concatenating strings, each with its own use cases. The dot operator and string interpolation are the PHP documentation approaches; implode() and .= are dependent on the context and str_replace() is usually the last step before moving toward a full templating system. Experiment with these methods to find the ones that best suit your coding style and project requirements. Happy coding!