php

PHP String Concatenation

Joining one or more string values with another string value or variable is called string concatenation. It is a common task when we want to print any output in a more meaningful way for the user. PHP has no built-in function to join string data like other standard programming languages. But the array of string values can be joined by using some PHP built-in functions. Different ways to join string values and an array of string values have been shown in this tutorial.

Pre-requisites:

The examples of this tutorial have been checked for PHP version 8+. So, you have to install PHP 8.0 and an apache server before practicing the examples of this tutorial.

Example-1: Concatenation Using Dot (.) Operator

String concatenation is normally done by using the dot (.) operator in PHP. Create a PHP file with the following script. Here, two string variables are declared and the dot (.) operator is used to combine these strings with other strings and print the combined string.

<?php
//Define a string
$string1 = "Charles Babbage";
//Define another string
$string2 = "Computer";
//Join string variables with a string value by using dot (.)
echo $string1." is considered as a father of ". $string2;
?>

Output:

The following output will appear after executing the above string.

Example-2: Concatenation Using Echo and Double Quotes (” “)

Two or more string values can be concatenated by using double quotes (” “). Create a PHP file with the following script. Here, two string variables have been concatenated by using double-quotes.

<?php
//Define two string values
$str1 = "PHP";
$str2 = " Programming";

//Combine string using double quotes
$concat_str = "$str1 $str2";

//Print the concatenated strings
echo $concat_str;

?>

Output:

The following output will appear after executing the above string.

Example-3: Concatenation Using Echo and Comma (,)

You can easily combine and print string data without using the dot (.) operator in PHP by using the echo function. Create a PHP file with the following script. Here, three-string variables have been declared, and “,” has been used to join the values of the variables with other strings by using the echo function. Each variable and string value are separated by comma (,) to combine. This type of joining can be done by using the echo function only.

<?php
//Define three string values
$name = "John";
$education = "MBA";
$profession = "Business man";
// Combine data using comma (,)
echo "$name"," is an ","$education. <br/>";
echo "He is a ","$profession.";
?>

Output:

The following output will appear after executing the above string.

Example-4: Concatenation Using printf() Function

You can use the printf() function also for concatenating string values in PHP. There are many types of specifiers are available in PHP to generate formatted output. Create a PHP file with the following script. Here, two string variables, $site, and $type have been declared. The printf() function uses ‘%s’ as type specifier to read string data. Two string variables have been added at the beginning and end of the string by adding two ‘%s’ in the first argument and two string variables as the other two arguments of the printf() function.

<?php
//Define two string values
$site = "linuxhint.com";
$type = "blog site";
//Concatenate string using printf() function
printf("%s is a very helpful %s",$site,$type);
?>

Output:

The following output will appear after executing the above string.

Example-5: Concatenation Using Shorthand Operator (.=)

Dot (.) can be used as shorthand operator for concatenating string data. Create a PHP file with the following script. Here, $n variable has been initialized to 0 and the ‘for’ loop has been used to combine the value of $n with a space in each iteration of the loop. The “.=” operator has been used inside the loop concatenate the values.

<?php
//Initialize the variable
$n = 0;
//Iterate the loop to concatenate 10 numbers with space
for($i=1; $i<10; $i++)
{
    $n .= " $i" ;
}
//Print the concatenated string value
echo $n;
>

Output:

The following output will appear after executing the above string.

Example-6: String Concatenation Using implode() Function

In all previous examples, the ways to concatenate two or more string values have been shown. Sometimes, it is required to concatenate the values of the array that may contain many string values. PHP has many built-in functions to do this type of task. The implode() function is one of them. Create a PHP file with the following script that will concatenate the array values with a space.

<?php
//Declare an array of string values
$arr_products = array("HDD","Monitor","Mouse","Keyboard","Scanner");
echo "<b>The product list:</b><br/><br/>";
//Combine the array values with the space using implode() function
$combine_str = implode(" ", $arr_products);
//Print the combined value
echo $combine_str;  
?>

Output:

The following output will appear after executing the above string.

Example-7: String Concatenation Using join() Function

The join() function is another way to combine array values in PHP. It works like the implode() function. Create a PHP file with the following script. Here, the join() function has been used to print each value of the array with a newline.

<?php
//Declare an array of string values of multiple words
$arr_customers = array('Muhmuda Akter','Fida bin jahid','Mehrub Hossain','Janifer Rahman');
echo "<b>The customer list:</b><br/><br/>";
//Combine the array values with newline using join() function
$combine_str = join("<br/>",$arr_customers);
//Print the combined value
echo $combine_str;  
?>

Output:

The following output will appear after executing the above string.

Conclusion

The string concatenation is an essential task for any programming language. The ways to concatenate string values, string values with the variables, and the string values of the array have been shown in this tutorial by simple examples. I hope the PHP users will be able to concatenate the string data properly in their script after reading this tutorial properly.

About the author

Fahmida Yesmin

I am a trainer of web programming courses. I like to write article or tutorial on various IT topics. I have a YouTube channel where many types of tutorials based on Ubuntu, Windows, Word, Excel, WordPress, Magento, Laravel etc. are published: Tutorials4u Help.