Skip to content
Home » How to join & append strings in PHP – String Concatenation Cheatsheet

How to join & append strings in PHP – String Concatenation Cheatsheet

Code Snippet: Connecting & Appending to strings in PHP


// 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.

Append & Concatenate strings in PHP with the concatenation 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, the strings are joined using the concatenation operator, and the result is assigned to a variable. 

We can also use the concatenation assignment operator =.. This operator joins two strings and assigns the value to the same variable. Let’s see how.


//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.

How to concatenate & change strings in PHP at a particular position

So far, we’ve seen the concatenation operator. Now, the operator can prepend or append one string to another. To join a string at a particular position in another string, we need to go the extra mile.

The idea is to split the first string first at a particular position and add the second string to it. 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.


$first_string = "I love coding in Javascript and Java";
 
$second_string = "PHP";
 
//We need to put the $second_string in the $first_string, between in and Javascript.
//The first part: I love coding in has offset of 0 and length 16.
//The second part: Javascript and Java has offset of 17, we can leave out the length parameter here
$concatenated_string = substr($first_string,0,16)." ".$second_string." ".substr($first_string,17);
 
echo $concatenated_string;
 
//OUTPUT
//I love coding in PHP Javascript and Java
 

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.

  1. substr($first_string,0,16) returns “I love coding in”;
  2. substr($first_string,17)    returns  “Javascript and Java”;

So, the statement reduces to

$concatenated_string = “I love coding in”.”  ”.”PHP”.”  ”.”Javascript and Java”;

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

  1. concatenated_string = substr($intro_string,0,$index)."PHP ".substr($intro_string,$index);
  2. concatenated_string = substr($intro_string,0,26)."PHP ".substr($intro_string,26);
  3. 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. 

That’s it for this article. Stay tuned for more helpful tutorials about PHP.

Want to explore more useful PHP tutorials?

We have many fun articles related to PHP. You can explore these to learn more about PHP.

What is PHP isset function and when to use it?

Convert array to string with PHP implode function

Make an associative array in PHP