PHP EOF (heredoc) Usage Instructions
PHP EOF (heredoc) is a method of defining a string in command-line shells (such as sh, csh, ksh, bash, PowerShell, and zsh) and programming languages (like Perl, PHP, Python, and Ruby).
Usage Overview:
- 1. It must be followed by a semicolon, otherwise the compilation will fail.
- 2. EOFAny other characters can be used instead, just ensure that the ending identifier is consistent with the starting identifier.
- 3. The ending identifier must be placed alone at the beginning of a line (i.e., it must start from the beginning of a line, with no whitespace or characters before or after it).
- 4. The starting identifier can be unquoted or enclosed in single or double quotes. Unquoted is equivalent to double-quoted, interpreting embedded variables and escape symbols. Single-quoted does not interpret embedded variables and escape symbols.
- 5. When the content needs embedded quotes (single or double quotes), there is no need to add escape characters; it escapes single and double quotes itself. This is similar to the usage of q and qq.
Example
echo <<<EOF
<h1>My First Heading</h1>
<p>My first paragraph.</p>
EOF;
// The ending must be on its own line with no spaces before or after
?>
Note:
1. Use<<<EOFthe start marker to start, and useEOFthe end marker to end. The ending marker must be written at the very beginning of a line, with no indentation or spaces, and there must be a semicolon at the end of the ending marker.
2. The start marker and the end marker are the same, such as the commonly used uppercaseEOT、EOD、EOFto represent it, but not limited to those (you can also use: JSON, HTML, etc.), as long as you ensure that the start marker and end marker do not appear in the body text.
3. Variables between the start marker and the end marker can be parsed normally, but functions cannot. In heredoc, variables do not need to use a concatenation symbol.or,to concatenate, as follows:
Example
$name="example";
$a= <<<EOF
"abc"$name
"123"
EOF;
// The ending must be on its own line with no spaces before or after
echo $a;
?>