In PHP, string concatenation refers to joining two or more strings into one. There are mainly two ways to achieve this:
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"
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"
Mastering these two string concatenation methods will help you handle PHP string operations more efficiently, whether it's simple joining or dynamic string building.