PHP EOF (heredoc) is a way to define a string in command-line shells (such as sh, csh, ksh, bash, PowerShell, and zsh) and programming languages (such as Perl, PHP, Python, and Ruby).
Usage overview:
1. It must be followed by a semicolon, otherwise the compilation will fail.
2. EOF can be replaced by any other character, as long as the end identifier is consistent with the start identifier.
3. The end mark must occupy its own line at the top of the line (that is, it must start from the beginning of the line, and cannot be connected with any blank spaces or characters before and after).
4. The start identifier can be without quotes or with single or double quotes. Without quotes, the effect is the same as with double quotes. Embedded variables and escape symbols will be interpreted. With single quotes, embedded variables and escape symbols will not be interpreted.
5. When the content requires embedded quotation marks (single quotation marks or double quotation marks), there is no need to add escape characters. Single and double quotation marks are escaped by itself. This is equivalent to the usage of q and qq.
<?php echo <<<EOF <h1>My first title</h1> <p>My first paragraph. </p> EOF; // The end needs to be on a separate line and there can be no spaces before and after it ?>
Notice:
1. Start with the <<<EOF
start tag and end with EOF
end tag. The end tag must be written at the top, without indentation or spaces, and there must be a semicolon at the end of the end tag.
2. The start tag and the end tag are the same. For example, capital EOT、EOD、EOF
are commonly used to represent them, but they are not limited to those (you can also use: JSON, HTML, etc.), as long as the start tag and end tag do not appear in the text. Can.
3. Variables located between the start tag and the end tag can be parsed normally, but functions cannot. In heredoc, variables do not need to be spliced with connectors .
or ,
as follows:
<?php $name = " codercto " ; $a = <<<EOF "abc" $name "123" EOF; // The end needs to be on a separate line and there can be no spaces before and after it echo $a ; ?>