How to Concat Strings in PHP to Combine Multiple Strings Together
I use several methods for concatenating strings in PHP. The dot operator is the standard method for combining multiple strings together. Sometimes, I also need PHP concat arrays. I use the implode function in these cases.
Check out the examples below to learn more about joining & combining PHP strings together.
Concat String Code Example: Combine & Append 2 or More Strings to Form a New String
// Here's a few quick ways to append to strings in php. // let's setup the base string. $myString = "Start with one string"; // Let's do a quick concatenation example. $myString = $myString . " and add 2nd string. "; // Let's show another way to append and concatenate a string in PHP $myString .= " Here's a 3rd string added. "; // What if we have another variable string that we want to add? $appendString = "Here's a 4th string."; $myString .= $appendString; // What if we wanted to include the $myString variable into another string. $newString = "What is this craziness? $myString."; // // Soo.. What happens if we echo'd our various appended, joined and changed string? echo "newString: $newString /r/n myString: $myString";
String concatenation means joining two strings. At the simplest, we can append or prepend two strings or do more than just that and insert strings within another string based on a particular position or a particular word.
Fortunately, PHP is quite robust when it comes to string concatenation. To concatenate strings in PHP, we have lots of flexibility and support. In this article, we’ll explore different options to concatenate two strings in PHP. So, let’s start without any further ado.
PHP Combine Strings Using the Concat Operator
In PHP, the dot operator
. is the concatenation operator. It concatenates two strings in PHP and returns a single string that could be stored in a variable or printed directly to the console. We have the freedom to append and prepend strings with this operator.
Let’s see the concatenation operator through an example.
//First string $first_string = "Hello World!!"; //Second string $second_string = " This is a PHP tutorial"; //Concatenation operator joins the two strings and pass it to a third variable $concatenated_string. $concatenated_string = $first_string.$second_string; //Print the concatenated string to the console. echo $concatenated_string; //OUTPUT //Hello World!! This is a PHP tutorial
In the example, we are concatenating strings using the . operator to assign the result to a variable.
We can also use the concatenation assignment operator
=.. to combine PHP string values. I can use this operator to join two string values and assigns the results to the same variable.
Let’s see how it works.
//First string $first_string = "Hello World!!"; //Second string $second_string = " This is a PHP tutorial"; //Concatenation operator joins the two strings using concatenation assignment operator $first_string .= $second_string; //Print the concatenated string to the console. echo $first_string; //OUTPUT //Hello World!! This is a PHP tutorial
In the example, the concatenation assignment operator joins the second string to the first one, ruling out the necessity of a third variable.
That’s one way to concatenate strings in PHP. What if we need to join a string at a specific position with another string? Let’s see that next.
Concatenate & Change String Values in PHP at a Specific Position
So far, we’ve seen the concatenation operator. Now, the operator can prepend or append one string to another. We need to go the extra mile to join a string at a particular position in another string.
The idea is to split the first string at a particular position and add the second string. To support this idea, we’ve substr function in PHP. You need to learn the syntax and parameters of the function through the PHP documentation for the substr function. A necessary step to excel as a developer is reading documentation. That’s why do not miss this part.
First, let’s see examples of the substr function before moving to the actual topic.
$greeting_string = "Hello World!!"; //OUTPUT: Hello echo substr($greeting_string,0,5); //OUTPUT: World!! echo substr($greeting_string,6); //OUTPUT: e echo substr($greeting_string,1,1); //OUTPUT: ello World!! echo substr($greeting_string,1);
As we can see, we have a lot of freedom to slice the string the way we want with the substr function. We’ll use substr with concatenation operator to join a string at a particular location in another string. Let’s see how.
Do you see how we have used substr and concatenation operator in the example. Let’s break down that line of code for more clarity.
substr($first_string,0,16) returns “I love coding in”;
So, the statement reduces to
So, the whole thing reduces to strings and concatenation operator, and we’ve already seen how that works. That’s the way to concatenate two strings in PHP at a particular position. Next, we’ll see how to concatenate strings in PHP before or after a specific word.
How to concatenate & change strings in PHP before or after a specific word
This example builds upon what we’ve already done with strpos. Here, we use a function to get the index of a word. This index would then be used in strpos to slice the string upto or beyond that word.
PHP has strpos function that gets the position of the first occurrence of a substring in a string. Once again, you’ve to see the documentation to understand the ins and outs of the strpos function.
To give you an idea, let’s look at a few examples of strpos.
//Index starts at 0. $intro_string = "My name is Bob"; //OUTPUT: 11 echo strpos($intro_string,"Bob"); //OUTPUT: 3 echo strpos($intro_string,"name"); //OUTPUT: 8 echo strpos($intro_string,"is");
Now, let’s understand through an example how to concatenate strings in PHP before and after a particular word using what we’ve seen so far.
$intro_string = "My name is Bob and I am a developer"; //strpos will return index position of developer. OUTPUT: 26 $index = strpos($intro_string,"developer"); //Reduces to: "My name is Bob and I am a"."PHP "."developer" $concatenated_string = substr($intro_string,0,$index)."PHP ".substr($intro_string,$index); echo $concatenated_string; //OUTPUT //My name is Bob and I am a PHP developer
The strpos function gets the index of “developer” in the $intro_string. The index is passed to the substr function to slice the string accordingly.
Let’s break down the code to understand it. The function executes as
concatenated_string = substr($intro_string,0,$index)."PHP ".substr($intro_string,$index);
concatenated_string = substr($intro_string,0,26)."PHP ".substr($intro_string,26);
concatenated_string = "My name is Bob and I am a"."PHP "."developer";
We can interpret the second and third forms of the statement based on what we’ve seen in the last two options. We hope you’ve grabbed the concept. The rest is practice.
PHP Concat Arrays: Use the PHP implode Functions
You can use the implode PHP function whenever you need to combine return the string values of PHP arrays into a single value. This is an efficient & common method used in lists & tables. If I want to return a list of fruits as an HTML unordered list then I will use the implode function. I can even check for empty strings.
Check out this example
Using Implode to Concat Arrays Into Strings in PHP
<?php // Create a list of fruits $fruits = ["apple", "banana", "orange", ""]; // Lets remove any empty strings first. $realFruits = array_filter($fruits, 'strlen'); // Now lets echo an HTML unordered list of my fruits. echo "<ul>"; echo "<li>"; echo implode("</li><li>", $realFruits); echo "</li>"; echo "</ul>";
This returns our desired results because of 2 simple functions (well, three, including strlen). We can pass the strlen function into the array_filter function. Once this is done, then we can implode the contents as needed.
PHP Fundamentals Article Series
This article is part of our series on learning the fundamentals of PHP web development. If you are stepping into your PHP journey. Check out the articles in this series to get deeper into PHP.
- Learn PHP Web Application Development
- Is PHP Easy to Learn
- Why does PHP have a bad reputation
- 5 PHP Template Engines Compared
- How to Filter Arrays in PHP with array_filter() Code Examples
- Object-Oriented PHP Programming Tutorial
- PHP Data Structures
- The 3 types of arrays in PHP
- Single vs Double Question Mark in PHP
- Join & Append PHP Strings
- Using PHP in_array
Learn the Fundamentals of Good Web Development
Please take a moment and sign up for our free email course on the fundamentals of good web development. Every week is packed with a roundup of articles on our site and from around the web, where we go deep into developing exceptional web applications. We have meetups, code reviews, slack chats, and more.