在看《深入PHP和JQeury开发》过程中,遇到字符串 操作符<<<,代码没有问题,但格式却要求特别严格,找到PHP手册上的实例,翻译过来,留作后用。
A string literal can be specified in four different ways:
- single quoted
- double quoted
- heredoc syntax
- nowdoc syntax (since PHP 5.3.0)
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