PHP String Concatenation

by Vincy. Last modified on June 2nd, 2021.

If we have enough familiarity with PHP inbuilt string and array functions, then, it will more helpful while doing code on PHP. In this series, we have seen some of such functions already, like, trimming, PHP functions for string case conversions and etc. And, recently, we have seen about how to extract part of an input string using PHP.

In PHP, there is no inbuilt function for concatenating more than one string, as like as, strcat() of C, concat() of MySQL and etc. Even though, it is a very easy job, with the use PHP operator which is especially for string concatenation. It is the very familiar dot(.) operator, that we have used in many examples.

For example, the dot operators are used to separate PHP variables from a hard-coded string, and to concatenate HTML tags with those variable, which is to be displayed to the browser as shown in the following code samples.

$query = "SELECT * FROM " . $table_name;


$heading = "PHP String Functions";
$heading_style = "<b>" . $heading . "</b>";

Though PHP doesn’t have any inbuilt functions for concatenating strings, we can use PHP implode() function for this purpose, where this function is especially for joining an array of values together, and they will be separated with the given delimiter. Let us see about this two method on PHP string concatenation with suitable examples.

String Concatenation using PHP dot Operator

The following PHP program uses the dot operator to add prefix for each element of the $table_name_array given. Then, by iterating over the $table_name_array variable, each of its element will be concatenated with the given string initialized for $table_prefix variable.

$table_prefix = "tbl";
$table_name_array = array("user","student","event","task");
foreach($table_name_array as $key=>$value) {
	$table_name_with_prefix[] = $table_prefix . $table_name_array[$key];
print "<PRE>";
print "</PRE>";

On executing above PHP script, it will print new array elements to the browser which contains the prefixed table names after string concatenation.

PHP implode() in String Concatenation

Though, PHP implode() functions is not dedicated for concatenation operation, we can use this optionally. Let us use this function to concatenate elements of the array $table_name_array, we have seen in the above example.

$table_name_array = array("user","student","event","task"); 
foreach($table_name_array as $key=>$value) { 
$table_name = implode($table_name_array); 
print $table_name;  

For concatenating the string elements of the given input array without any delimiter, we have omitted this optional argument for the PHP implode() function.


  • PHP implode() function can only join the array elements together; Rather, it can not perform this concatenation between these array elements with any explicitly initialized string variable or with any hard coded string.
  • Compare with this implode() function, string concatenation using dot operator is preferable technique, since, implode() will take more time and less optimized to perform concatenation operation than PHP concatenation operator.

Comments to “PHP String Concatenation”

  • Michael says:

    Hi Vincy, I am a beginner in PHP and your tutorials are simple and easy to understand. This tutorial on String concatenation is easy to follow. Thanks, keep writing more.

Leave a Reply

Your email address will not be published. Required fields are marked *

↑ Back to Top