1. Concatenation Operator (""."")
The concatenation operator in PHP is represented by a dot ("."). It joins two or more strings into a single string. The concatenation operator can be used with string literals, variables, or a combination of both.
The basic syntax for using the concatenation operator is :
$string1 = "Hello";
$string2 = "world!";
$result = $string1 . " " . $string2;
echo $result;

You can also try this code with Online PHP Compiler
Run Code
Output
Hello world!
In this example, we have two string variables, $string1 and $string2. We use the concatenation operator to join these strings together, with a space in between them, and assign the result to a new variable called $result. Finally, we use echo to output the concatenated string.
You can concatenate as many strings as you need using the concatenation operator.
For example:
$firstName = "Harsh";
$lastName = "Singh";
$age = 25;
$intro = "My name is " . $firstName . " " . $lastName . ", and I am " . $age . " years old.";
echo $intro;

You can also try this code with Online PHP Compiler
Run Code
Output
My name is Harsh Singh, and I am 25 years old.
In this example, we concatenate multiple string literals and variables to create a complete sentence stored in the $intro variable.
2. Concatenating Assignment Operator ("".="")
In addition to the concatenation operator, PHP also provides a concatenating assignment operator (".="). This operator allows you to concatenate a string to an existing string variable, modifying the variable's value in place.
The basic syntax for using the concatenation assignment operator is:
$string = "Hello";
$string .= " world!";
echo $string;

You can also try this code with Online PHP Compiler
Run Code
Output:
Hello world!
In this example, we start with a variable $string that holds the value "Hello". We then use the concatenation assignment operator to append the string " world!" to the existing value of $string. The resulting value of $string is now "Hello world!".
The concatenation assignment operator is handy when you need to build a string incrementally or modify an existing string variable.
Let’s take an another example:
$message = "Welcome";
$message .= " to PHP";
$message .= " string concatenation!";
echo $message;

You can also try this code with Online PHP Compiler
Run Code
Output:
Welcome to PHP string concatenation!
In this example, we use the concatenation assignment operator multiple times to gradually build the $message string. Each time the operator is used, the new string is appended to the existing value of $message.
Using the concatenation assignment operator can often result in more concise and readable code than repeatedly using the concatenation operator.
$string1 = "Hello";
$string2 = " world!";
$string1 = $string1 . $string2; // Using concatenation operator
echo $string1;
$string1 = "Hello";
$string1 .= " world!"; // Using concatenation assignment operator
echo $string1;

You can also try this code with Online PHP Compiler
Run Code
Output
Hello world!
Hello world!
Both approaches achieve the same result, but the concatenation assignment operator provides a more streamlined way to modify the string variable directly.
Frequently Asked Questions
What is the difference between single quotes & double quotes when creating strings in PHP?
Single quotes treat strings as literals, while double quotes interpret variables & escape sequences within the string.
Can you concatenate strings with other data types in PHP?
Yes, PHP automatically converts other data types to strings when concatenating them with a string.
Is there a limit to the number of strings you can concatenate in PHP?
There is no specific limit, but consider readability, maintainability, & performance when concatenating many strings.
Conclusion
In this article, we discussed string concatenation in PHP. We learned about the concatenation operator (".") & the concatenation assignment operator (".="), which allows us to combine strings efficiently with proper examples. With string concatenation, you can create dynamic & flexible text-based content in your PHP projects.
You can also check out our other blogs on Code360.