Home > Backend Development > PHP Tutorial > Detailed explanation of simple examples of php string connection

Detailed explanation of simple examples of php string connection

伊谢尔伦
Release: 2023-03-11 16:02:01
Original
1964 people have browsed it

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&#39;s BLOG!";
  //连接上面两个字符串 中间用空格分隔
  $str3 = $str1 . " " . $str2;
  //输出连接后的字符串
  echo $str3;
  /*
   该段代码执行后浏览器页面将显示
       "Hello World! Welcome to HutaoW&#39;s BLOG!"
  */
?>
Copy after login


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}&#39;s BLOG!";
  //输出$str字符串
  echo $str;  /*
   该段代码执行后浏览器页面将显示
       "Welcome to HutaoW&#39;s BLOG!"
  */
?>
Copy after login


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!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template