PHP EOF (heredoc) Usage
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 (like Perl, PHP, Python, and Ruby).
Usage overview:
-
- It must be followed by a semicolon, otherwise it will not compile.
-
- EOF You can use any other character instead, as long as the closing identifier matches the opening identifier.
- 3. The closing identifier must be on its own line at the very beginning (i.e., it must start from the beginning of the line, with no whitespace or characters before or after it).
-
- The opening identifier can be without quotes or with single/double quotes. Without quotes and with double quotes have the same effect: they interpret embedded variables and escape sequences; with single quotes, embedded variables and escape sequences are not interpreted.
-
- When the content needs embedded quotes (single or double), no escape characters are needed; it escapes single and double quotes by itself, which is equivalent to the q and qq usages.
Example
My first headingMy first paragraph.
EOF ; // the end must be on its own line with no spaces before or after ?>Note:
1. Start with << 2. The opening and closing markers are the same, e.g. the commonly used uppercase EOT, EOD, EOF to represent them, but not limited to those (you can also use: JSON, HTML, etc.), as long as the opening and closing markers do not appear in the body text. 3. Variables between the opening and closing markers can be parsed normally, but functions cannot. In heredoc, variables do not need to be concatenated with the . or , operator, as shown below:Example