Heim  >  Artikel  >  php教程  >  PHP中的双引号和单引号的应用

PHP中的双引号和单引号的应用

WBOY
WBOYOriginal
2016-06-13 09:33:121636Durchsuche

  无论是书写 JavaScript 还是 PHP,总习惯于使用单引号。但周末在家 coding 的时候碰到个问题,需要通过 PHP 过滤字符串中的换行符,按照下面的方法操作:

  $out = str_replace(array('\r\n', '\r', '\n'), '', $out);PHP 提供三种定义字符串的方法:单引号、双引号、本地文档(英文叫做 here document 或者 heredoc)。

  单引号:

  使用单引号是最高效的方法,因为 PHP 不会检查单引号字符串中的内置变量和转义序列,需要转义的字符只有反斜杠和单引号本身。

  双引号:

  会检查内置变量和转义序列,但不能识别转义的单引号。这也正说明了开始那段代码的错误之处,正确的做法是使用双引号来定义换行的转义序列:

  $out = str_replace(array("\r\n", "\r", "\n"), '', $out);本地文档:

  检查所有的内置变量和转义序列,双引号无需转义。例如:

  echo

  this is a "here document" example.

  just for test.

  EOT;简单记录下,加深印象。

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