Heim  >  Artikel  >  Backend-Entwicklung  >  PHP字符串的连接_PHP教程

PHP字符串的连接_PHP教程

WBOY
WBOYOriginal
2016-07-13 17:45:501001Durchsuche

 

很多时候我们需要将几个字符串连接起来显示,在PHP中,字符串之间使用“点”来连接,也就是英文中的句号”.”,具体使用方式如下:

 

  //定义字符串

  $str1 = "Hello World!";

  $str2 = "Welcome to HutaoW's BLOG!";

 

  //连接上面两个字符串 中间用空格分隔

  $str3 = $str1 . " " . $str2;

 

  //输出连接后的字符串

  echo $str3;

  /*

   该段代码执行后浏览器页面将显示

       "Hello World! Welcome to HutaoW's BLOG!"

  */

?>

 

连接字符串还有另外一种方法,有点像C中”printf”的占位符,不过PHP是直接把变量名写到占位符上了。使用方法就是在变量前后加上大括号,这样显示时,字符串中没有用大括号括起来的部分依旧会直接输出,而括起来的部分会根据变量名替换输出相应的字符串。可以看下面的例子更清晰些,注意其中下划线的部分:

 

  //定义待插入的字符串

  $author = "HutaoW";

 

  //生成新的字符串

  $str = "Welcome to {$author}'s BLOG!";

 

  //输出$str字符串

  echo $str;  /*

   该段代码执行后浏览器页面将显示

       "Welcome to HutaoW's BLOG!"

  */

?>

摘自:Shine的圣天堂

www.bkjia.comtruehttp://www.bkjia.com/PHPjc/478624.htmlTechArticle很多时候我们需要将几个字符串连接起来显示,在PHP中,字符串之间使用点来连接,也就是英文中的句号.,具体使用方式如下: ?php //定义...
Stellungnahme:
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn