Current Location: Home> Latest Articles> Detailed Guide to PHP String Concatenation: Easily Combine Strings

Detailed Guide to PHP String Concatenation: Easily Combine Strings

M66 2025-08-05

Two Methods to Concatenate Strings in PHP

In PHP, string concatenation refers to joining two or more strings into one. There are mainly two ways to achieve this:

Dot Operator (.)

The dot operator (.) is the basic operator used to concatenate strings in PHP. It directly joins two strings to form a new string.

Example code:

$str1 = "Hello";
$str2 = "World";
$result = $str1 . $str2;  // Result is "HelloWorld"

Concatenation Assignment Operator (.=)

The concatenation assignment operator (.=) appends a string to the end of an existing string. It is equivalent to adding the right-hand string to the left-hand string.

Example code:

$str1 = "Hello";
$str1 .= "World";  // Equivalent to $str1 = $str1 . "World"

Usage Notes

  • Both the dot operator and concatenation assignment operator achieve the same concatenation result.
  • The concatenation operator has higher precedence than the assignment operator, so be mindful of execution order when using the concatenation assignment operator.
  • PHP strings are immutable, meaning each concatenation creates a new string copy; frequent concatenation may impact performance.

Summary

Mastering these two string concatenation methods will help you handle PHP string operations more efficiently, whether it's simple joining or dynamic string building.