在看《深入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

PHP 字符串 操作符<<< 使用的注意事项的更多相关文章

  1. PowerShell 字符串操作符

    字符串操作符 格式化操作符 –F 在PowerShell文本操作符中非常重要,经常被用来增强数字类型和日期类型的可读性: "{0} diskettes per CD" -f (72 ...

  2. Java字符串split函数的注意事项

    Java字符串的split方法可以分割字符串,但和其他语言不太一样,split方法的参数不是单个字符,而是正则表达式,如果输入了竖线(|)这样的字符作为分割字符串,会出现意想不到的结果, 如, Str ...

  3. 字符串拼接data-id时注意事项

    今天测试下一个ajax请求,结果后台接收不到data-id的数据,导致后台无法进行正确的数据库查询. 我的评论页面是使用字符串拼接后,再放到页面里的,其中有关data-id的部分是这样的: '< ...

  4. C++的string类型和继承C语言风格的字符串的区别与注意事项

    1.尽可能地在C++程序中使用string,不要使用继承而来的C语言风格的字符串,会出现许多安全问题. 2.C语言的字符串风格,是以空字符结束的,在C++的头文件cstring中定义了C语言风格的字符 ...

  5. 【Python】字符串操作符

  6. Chapter3_操作符_其他操作符

    对java中其他操作符及一些注意事项的总结 (1)按位操作符 按位操作符操作基本整数类型中的单个二进制位,有与(&),或(|),非(~).按位操作符还可以和等号(=)联合使用,如~=,& ...

  7. Java之路(二) 操作符

    操作符比较简单,这里只点一下注意事项和要点,请牢记. 操作符接受一个或多个参数,并生成一个新值. Java中,几乎所有的操作符都只能操作基本类型. 例外是 = == 和 !=,它们可以操作所有的对象. ...

  8. javaScript常用运算符和操作符总结

    javaScript常用运算符和操作符总结 类别 操作符 算术操作符 +. –. *. /. %(取模) 字符串操作符 + 字符串连接   +=字符串连接复合 布尔操作符 !. &&. ...

  9. shell脚本之字符串测试表达式

    1.字符串测试操作符 字符串测试操作符的作用有:比较两个字符串是否相同.字符串的长度是否为零,字符串是否为NULL(注:bash区分零长度字符串和空字符串等) 下表为常用字符串操作符 也可以通过man ...

随机推荐

  1. Web大文件上传(断点续传)控件-Xproer.HttpUploader6-安装教程

      安装教程: IE8控件安装教程 IE9控件安装教程 Firefox控件安装教程 Chrome控件安装教程 Chrome 45+控件安装教程 相关问题: 提示Runtime Error错误 360拦 ...

  2. Finding Memory Leaks with SAP Memory Analyzer

    Introduction There is a common understanding that a single snapshot of the java heap is not enough f ...

  3. 多线程的那点儿事(之windows锁)

    在windows系统中,系统本身为我们提供了很多锁.通过这些锁的使用,一方面可以加强我们对锁的认识,另外一方面可以提高代码的性能和健壮性.常用的锁以下四种:临界区,互斥量,信号量,event. (1) ...

  4. 深入理解java虚拟机(十四)正确利用 JVM 的方法内联

    在IntelliJ IDEA里面Ctrl+Alt+M用来拆分方法.选中一段代码,敲下这个组合,非常简单.Eclipse也用类似的快捷键,使用 Alt+Shift+M.我讨厌长的方法,提起这个下面这个方 ...

  5. BZOJ 1977 严格次小生成树

    小C最近学了很多最小生成树的算法,Prim算法.Kurskal算法.消圈算法等等.正当小C洋洋得意之时,小P又来泼小C冷水了.小P说,让小C求出一个无向图的次小生成树,而且这个次小生成树还得是严格次小 ...

  6. ThinkJS 中的Logic层

    第一个为什么需要Logic层: 当在 Action 里处理用户的请求时,经常要先获取用户提交过来的数据,然后对其校验,如果校验没问题后才能进行后续的操作:当参数校验完成后,有时候还要进行权限判断等,这 ...

  7. RzToolbutton用法

  8. Android-主题

    主题分为两种: 第一种:使用Android系统提供的主题    第二种:自己定义主题 样式 与 主题 区分理解 样式是控制(View的子类风格)控件风格 或者 是(ViewGroup的子类风格)布局风 ...

  9. Android-自定义RadioButton

    1.控件RadioButton需要用RadioGroup包裹起来,才能使用2.RadioButton必须设置ID才能实现单选功能3.RadioGroup有方向(垂直方向 和 水平方向)默认是垂直方向 ...

  10. 基于SSH的网上购物商城系统-JavaWeb项目-有源码

    开发工具:Myeclipse/Eclipse + MySQL + Tomcat 项目简介: 基于WEB的网上购物系统主要功能包括:前台用户登录退出.注册.在线购物.修改个人信息.后台商品管理等等.本系 ...