Skip to content
Home » How to Concatenate Strings in PHP

How to Concatenate Strings in PHP

Summary

PHP uses dot notation to concatenate strings in PHP. You can also use variables inside of double quotes without the need of concatenation. Check out the article for examples.

There is 1 primary method to concatenate strings in PHP. Add a period between each of your strings when assigning the variable that will connect them together.

Are you needing to connect multiple strings together into a single unit? The best solution is to connect them together through dot separators as such:

$myValue = "string A" . " string B " . " string c ";
echo $myValue;
// The result will be 'string A string b string c'

You will see the above code a good bit in examples but it isn’t practical. Why would I not just write it all as one string? It is much more common to find other use cases where you’re concatenating strings onto string variables, mixing strings with variables, or need to create a large string from a large array of strings.

These situations are a little more complex and in-depth.

Concatenate an existing string variable with a new string

One common use case that I find when writing my PHP code is that I need to concatenate an existing variable with a new string. This happens a good bit when I am outputting a large amount of HTML to the browser. You don’t want to echo strings everywhere whenever you need to do logic. Collect all your content together and then echo or render it at the end.

Let’s say that I have a list of 3 pies to begin, and then I need to add another pie to the list because your crazy neighbor with allergies decided to come to your party. How would you do this?

Simple:

$myPies = "Apple, Blueberry, Peach";
$myPies .= ", Pumpkin";

echo $myPies;
// renders: "Apple, Blueberry, Peach, Pumpkin"

Important note: Consider spaces, punctuations, grammar, etc when concatenating strings. It’ll get you every time.

The above code will add Pumpkin to the end of your wonderful comma-delimited list of delicious pies.

Ok, but what if you want to make Pumpkin the first pie because it’s Thanksgiving?

Simple: Assign a new variable or reassign the original variable with the new string with a dot separating the old original.

$myPies = "Apple, Blueberry, Peach";
$yummyPies = "Pumpkin, " . $myPies;

echo $yummyPies;
// Renders 'Pumpkin, Apple, Blueberry, Peach'

Concatenate multiple strings with a dynamic string

The above example was pretty simple and basic, but let’s take our string example to a new level.

I have 3 pies. I know the first will always be Apple and the last will always be Peach, but the second can be any number of pies. What do I do in that example?

Well, there are 2 solutions. A basic solution that you may have already have imagined but a cooler method also exists. Let’s start with the boring first option.

$myDynamicPie = "Chocolate";
$allPies = "Apple, " . $myDynamicPie . ", Peach";
echo $allPies;
// renders 'Apple, Chocolate, Peach'

The above is a simple solution that gets the job done. You have string A, a variable string and then end with another string. I may use this approach whenever I need to put in a dynamic variable inside of an H1 tag.

The problem is that it is error-prone and can be difficult to read. What if I could accomplish the same thing without needing my dot separator?

Blasphemy!

But you can.

$myDynamicPie = "Pumpkin";
$allPies = "Apple, $myDynamicPie, Peach";
echo $allPies;
// renders 'Apple, Pumpkin, Peach'

Do you see what I did there? You can echo your variable right inside your list of pies without a dot separator. This will make your code more readable and less prone to error.

The official big dev word for the above is string interpolation. Interpolation is technically different than concatenation, but it solves the same problem in this use case.

Note: You MUST use double quotes with your PHP string interpolations. You’ll just print the variable name if you use single quotes.

$myDynamicPie = 'Banana';
$allPies = 'Apple, $myDynamicPie, Peach';
echo $allPies;
// renders 'Apple, $myDynamicPie, Peach';

Shortcut: Concatenating many strings together in PHP

ok, the above is all well and good, but you may have noticed something.

I was manually comma-delimiting my list of pies together within my string. This is fine for this use case BUT it’s not the real world is it? I’ll likely have a list of 5,000 pies that I need to connect together. There must be a better way to connect strings together.

Of course, there is a way to join many strings together in PHP. You could do the following:

$pies = "Apple, ";
$pies .= "Banana, ";
$pies .= "Peach, ";
$pies .= "Pumpkin";
echo $pies;
// renders "Apple, Banana, Peach, Pumpkin"

But that is going to get ugly and TIME-CONSUMING real fast.

The smarter approach to take when concatenating large amounts of strings is to insert them all into an array and then implode them all into a single string.

$pies = ["Apple", "Banana", "Peach", "Pumpkin"];
echo implode(", ", $pies);
// renders 'Apple, Banana, Peach, Pumpkin'

See the difference and improvement there? Implode is a wonderful function. It creates a string from an array list. The first parameter in implode($separator, $array) is the separator and then the second is the array that you want to use.

The above is the smart approach to connecting many large strings together.

Concatenating Strings in PHP

In the end, dot separators are a great method to concatenate strings in PHP, but as you see above that it may or may not be the right solution for your needs. Depending on the size of your strings and use case, there may be a better approach to take to improve readability, readability, and performance. I really consider the use case and end result whenever I am evaluating the best purpose of joining my strings together. It may be that concatenation is the best. On the other hand, array joins or interpolation is better.