PHP String Concatenation

If we have enough familiarity with PHP in-built 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 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 hardcoded string, and to concatenate HTML tags with those variable, which is to be displayed to the browser as shown in the following code samples.

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

//(AND)

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

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

php_string_concatenation

String Concatenation using PHP dot Operator

The following PHP program uses 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.

<?php
$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_r($table_name_with_prefix);
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.

<?php 
$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.

Note:

  • 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 hardcoded 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.

This PHP code tutorial was published on July 8, 2013.

↑ Back to Top