在看《深入PHP和JQeury开发》过程中,遇到字符串 操作符<<<,代码没有问题,但格式却要求特别严格,找到PHP手册上的实例,翻译过来,留作后用。

A string literal can be specified in four different ways:

Single quoted

The simplest way to specify a string is to enclose it in single quotes (the character ').

To specify a literal single quote, escape it with a backslash (\). To specify a literal backslash, double it (\\). All other instances of backslash will be treated as a literal backslash: this means that the other escape sequences you might be used to, such as \r or \n, will be output literally as specified rather than having any special meaning.

Note: Unlike the double-quoted and heredoc syntaxes, variables and escape sequences for special characters will not be expanded when they occur in single quoted strings.

<?php
echo 'this is a simple string'; echo 'You can also have embedded newlines in
strings this way as it is
okay to do'; // Outputs: Arnold once said: "I'll be back"
echo 'Arnold once said: "I\'ll be back"'; // Outputs: You deleted C:\*.*?
echo 'You deleted C:\\*.*?'; // Outputs: You deleted C:\*.*?
echo 'You deleted C:\*.*?'; // Outputs: This will not expand: \n a newline
echo 'This will not expand: \n a newline'; // Outputs: Variables do not $expand $either
echo 'Variables do not $expand $either';
?>

Heredoc

A third way to delimit strings is the heredoc syntax: <<<. After this operator, an identifier is provided, then a newline. The string itself follows, and then the same identifier again to close the quotation.

The closing identifier must begin in the first column of the line. Also, the identifier must follow the same naming rules as any other label in PHP: it must contain only alphanumeric characters and underscores, and must start with a non-digit character or underscore.

Warning

It is very important to note that the line with the closing identifier must contain no other characters, except possibly a semicolon (;). That means especially that the identifier may not be indented, and there may not be any spaces or tabs before or after the semicolon. It's also important to realize that the first character before the closing identifier must be a newline as defined by the local operating system. This is \n on UNIX systems, including Mac OS X. The closing delimiter (possibly followed by a semicolon) must also be followed by a newline.

If this rule is broken and the closing identifier is not "clean", it will not be considered a closing identifier, and PHP will continue looking for one. If a proper closing identifier is not found before the end of the current file, a parse error will result at the last line.

Heredocs can not be used for initializing class properties. Since PHP 5.3, this limitation is valid only for heredocs containing variables.

注意在结尾标示符那一行不能包还其他的字符,除了分号(;)。这就意味着标示符不能缩进,并且在分号之前或者之后都不能Space或者Tab。

并且,在结尾标示符之前的第一个字符必须是被本操作系统所定义的一个新行。在Unix系统中,为\n.并且结束标示符后也必须紧跟着换行符。

即必须为如下形式,特别注意虽然显示都是空白,但务必要确定空格为“换行符”:

{

...

...

\n

CLOSING_IDENTIFER;\n

}

原文链接:http://www.php.net/manual/en/language.types.string.php#language.types.string.syntax.heredoc

最新文章

  1. .net 分布式架构之任务调度平台
  2. ubuntu中用户使用的shell如何指定
  3. 【原】小搞一下 javascript算法
  4. qt5中文代码编码编译问题
  5. 如何做出透明背景的flash动画
  6. Linux下使用 Memory Analyzer
  7. 图解集合2:LinkedList
  8. RecyclerView 介绍 01
  9. 理解vmp
  10. Spring MVC 的汉字乱码问题
  11. ASP.NET MVC 5使用CrystalReport(水晶报表)
  12. [Java] 垃圾回收机制 ( Garbage Collection ) 简介
  13. SyntaxError: Non-ASCII character '\xe2' in file 编码错误
  14. struts2-----新建项目
  15. SourceTree管理工具的一些使用总结
  16. vhdl 数组
  17. [转]Python中的eval()、exec()及其相关函数
  18. [转]SpringMVC+ Mybatis 配置多数据源 + 手动切换数据源
  19. 【UML】:时序图
  20. Android逆向 编写一个Android程序

热门文章

  1. _I、_O、_IO的含义
  2. delphi 中封装的VCl窗体Tab键响应问题
  3. jQuery,Table表头固定插件chromatable存在的问题及解决办法
  4. C#连接SQL server数据库
  5. [网络流24题] 最长K可重区间集问题
  6. 201621123012《Java程序设计》第13次学习总结
  7. 波利亚(Polya)罐子模型
  8. for循环 | range 对象
  9. php数据库编程---mysql扩展库
  10. jmeter聚合报告详解