Home >Backend Development >PHP Tutorial >PHP string concatenation_PHP tutorial
Many times we need to connect several strings for display. In PHP, "dots" are used to connect strings, which is the period "." in English. The specific usage is as follows:
//Define string
$str1 = "Hello World!";
$str2 = "Welcome to HutaoW's BLOG!";
//Concatenate the two strings above and separate them with spaces
$str3 = $str1 . " " . $str2;
//Output the connected string
echo $str3;
/*
After this code is executed, the browser page will display
"Hello World! Welcome to HutaoW's BLOG!"
*/
?>
There is another way to connect strings, which is a bit like the placeholder of "printf" in C, but PHP writes the variable name directly to the placeholder. The method of use is to add curly brackets before and after the variable. When displayed, the part of the string that is not enclosed by curly brackets will still be output directly, and the enclosed part will be replaced and output with the corresponding string according to the variable name. You can look at the following example to make it clearer, pay attention to the underlined part:
//Define the string to be inserted
$author = "HutaoW";
//Generate new string
$str = "Welcome to {$author}'s BLOG!";
//Output $str string
echo $str; /*
After this code is executed, the browser page will display
"Welcome to HutaoW's BLOG!"
*/
?>
Excerpted from: Shine’s Holy Paradise