What are the methods for string concatenation in PHP?

In PHP, there are several methods for string concatenation.

  1. Use the “.” operator for concatenation, for example:
$str1 = "Hello";
$str2 = "World";
$result = $str1 . $str2;
echo $result; // 输出:HelloWorld
  1. Connect multiple strings using the “.” operator, for example:
$str1 = "Hello";
$str2 = " ";
$str3 = "World";
$result = $str1 . $str2 . $str3;
echo $result; // 输出:Hello World
  1. collapse()
$arr = array("Hello", "World");
$result = implode(" ", $arr);
echo $result; // 输出:Hello World
  1. formatting function
$str1 = "Hello";
$str2 = "World";
$result = sprintf("%s %s", $str1, $str2);
echo $result; // 输出:Hello World

These are common string concatenation methods in PHP, and you can choose the one that suits your needs best.

bannerAds