Home >Backend Development >PHP Tutorial >Detailed explanation of simple examples of php string connection
Many times we need to connect several strings for display. In PHP, "dots" are used to connect strings, that is, in English period ".", the specific usage is as follows:
<?php //定义字符串 $str1 = "Hello World!"; $str2 = "Welcome to HutaoW's BLOG!"; //连接上面两个字符串 中间用空格分隔 $str3 = $str1 . " " . $str2; //输出连接后的字符串 echo $str3; /* 该段代码执行后浏览器页面将显示 "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 symbol, but PHP directly writes the variable name 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:
<?php //定义待插入的字符串 $author = "HutaoW"; //生成新的字符串 $str = "Welcome to {$author}'s BLOG!"; //输出$str字符串 echo $str; /* 该段代码执行后浏览器页面将显示 "Welcome to HutaoW's BLOG!" */ ?>
The above is the detailed content of Detailed explanation of simple examples of php string connection. For more information, please follow other related articles on the PHP Chinese website!