单引号字符串

只能转义斜杠(\)和单引号(\’)

双引号字符串

可以转义常见的符号,并且自带模板字符串功能

  1. <?php
  2. $name = 'Chinese';
  3. $sentence = "I'm $name"!;
  4. echo $sentence;
  5. //输出为:I'm Chinese!
  6. ?>

Heredoc

类似于双引号字符串,但可以跨越多行,并且引号不会被转义,可以用$变量名或{$变量名}插入字符串,由于匹配机制的问题,建议都加上{}避免出错

  1. <?php
  2. $name = "Leng Feng";
  3. $chinese = 'Chinese';
  4. $sentence = <<< EOT
  5. My name is $name, I'm {$chinese}!
  6. EOT;
  7. echo $sentence;
  8. //输出为:My name is Leng Feng,I'm Chinese!
  9. ?>

Nowdoc

类似于多行的单引号字符串,不进行解析,一般用于插入大段的静态文本。

  1. <?php
  2. $name = "Leng Feng";
  3. $chinese = 'Chinese';
  4. $sentence = <<< 'EOT'
  5. My name is $name, I'm {$chinese}!
  6. EOT;
  7. echo $sentence;
  8. //输出为:My name is $name, I'm {$chinese}!
  9. ?>