1,从 HTML 中分离
凡是在一对开始和结束标记之外的内容都会被 PHP 解析器忽略,这使得 PHP 文件可以具备混合内容。 可以使 PHP 嵌入到 HTML 文档中去,如下例所示。
This is going to be ignored by PHP and displayed by the browser.
This will also be ignored by PHP and displayed by the browser.
这将如预期中的运行,因为当 PHP 解释器碰到 ?> 结束标记时就简单地将其后内容原样输出(除非马上紧接换行 - 见指令分隔符)直到碰到下一个开始标记;
例外是处于条件语句中间时,此时 PHP 解释器会根据条件判断来决定哪些输出,哪些跳过。
见下例,注意if 和 else 后面的 : 冒号
使用条件结构:
This will show if the expression is true.
Otherwise this will show.
上例中 PHP 将跳过条件语句未达成的段落,即使该段落位于 PHP 开始和结束标记之外。
由于 PHP 解释器会在条件未达成时直接跳过该段条件语句块,(:?> 跳出了 PHP 模式并返回了 HTML 模式) 因此 PHP 会根据条件来忽略之。
要输出大段文本时,跳出 PHP 解析模式通常比将文本通过 echo 或 print 输出更有效率。
getData())):?>
-------------------------------------------------- ----------------------------------
The closing tag in a piece of PHP code implicitly represents a semicolon
The last line in a PHP snippet does not need to end with a semicolon
If a newline follows, the end of the line is included in the snippet's closing tag.
echo "This is a test";
?>
Note:
The end tag of the PHP code segment at the end of the file is not required
Sometimes it is better to omit it when using include or require so that unwanted whitespace does not appear at the end of the file and the response headers can still be output later.
It's also convenient when using output buffering, so you don't see unwanted whitespace characters generated by include files.
342432353
otherwise this will show.
342432353
otherwise this will show.
-------------------------------------------------- ----------------------------------
echo "This is a test"; // This is a one-line c++ style comment
Single-line comments only comment to the end of the line or to the current block of PHP code, whichever comes first.
This means that HTML code after // ... ?> or # ... ?> will be displayed
:?> exits PHP mode and returns to HTML mode, // or # does not affect this.
C-style comments end when the first */ is encountered.
/*
echo "This is a test"; /* This comment will cause a problem */
echo 'kdfjal';
?>
An error will be reported and the output will be blank
-------------------------------------------------- ----------------------------------
String string
Note: The maximum size of string can reach 2GB.
Grammar
A string can be expressed in 4 ways:
single quote
Double quotes
Heredoc syntax structure
Nowdoc syntax structure (since PHP 5.3.0)
-------------------------------------------------- ----------------------------------
Single quote
To express a single quote itself, you need to escape it by adding a backslash () in front of it.
To express a backslash by itself, use two backslashes (\).
Any other form of backslash will be treated as the backslash itself: that is, if you want to use other escape sequences such as r or n, it does not mean any special meaning, just the two characters themselves.
echo 'this is a simple string';
echo 'You can also have embedded newlines in
strings this way as it is
okay to do';
echo 'Arnold once said: "I'll be back"';
echo 'You deleted C:\*.*?';
echo 'You deleted C:*.*?';
echo 'This will not expand: n a newline';
echo 'Variables do not $expand $either';
?>
-------------------------------------------------- ----------------------------------
Double quotes
If the string is enclosed in double quotes ("), PHP will parse some special characters:
Escape character
Sequence Meaning
n Line feed (LF or 0x0A (10) in ASCII character set)
r Carriage return (CR or 0x0D (13) in ASCII character set)
t Horizontal tab character (HT or 0x09 (9) in the ASCII character set)
v Vertical tab (VT or 0x0B (11) in ASCII character set) (since PHP 5.2.5)
e Escape (ESC or 0x1B (27) in ASCII character set) (since PHP 5.4.0)
f Form feed (FF or 0x0C (12) in ASCII character set) (since PHP 5.2.5)
\ Backslash
$ Dollar mark
" " Double quotation marks
[0-7]{1,3} What matches this regular expression sequence is a character expressed in octal notation
x[0-9A-Fa-f]{1,2} What matches this regular expression sequence is a character
Like single-quoted strings, escaping any other characters will cause the backslash to be displayed. Prior to PHP 5.1.1, backslashes in {$var} were not displayed.
The most important feature of strings defined with double quotes is that variables will be parsed. See variable parsing for details.
-------------------------------------------------- ----------------------------------
Heredoc structure
The third way to express a string is to use the heredoc syntax structure: <<<. After the operator, provide an identifier and then a newline. Next is the string string itself, and finally the identifier defined earlier is used as the end mark.
The identifier quoted at the end ofmust be in the first column of the line, and the naming of the identifier must follow the same PHP rules as other tags: it can only contain letters, numbers, and underscores, and must start with letters and Begin with an underscore.