標籤:style blog http io color os ar 使用 for
在看《深入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.
<?phpecho ‘this is a simple string‘;echo ‘You can also have embedded newlines in strings this way as it isokay 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 newlineecho ‘This will not expand: \n a newline‘;// Outputs: Variables do not $expand $eitherecho ‘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 字串 操作符<<< 使用的注意事項