YouTip LogoYouTip

Php Eof Heredoc

PHP EOF(heredoc) Usage Guide

PHP EOF(heredoc) Usage Guide

PHP EOF(heredoc) is a method for defining a string in command-line shells (like 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 it will not compile.
  • 2. EOF can be replaced with any other character, as long as the ending identifier matches the starting identifier.
  • 3. The ending identifier must be on its own line, starting at the beginning of the line (i.e., it must start at the beginning of the line, with no whitespace or characters before or after it).
  • 4. The starting identifier can be without quotes or with single or double quotes. Without quotes has the same effect as with double quotes, interpreting embedded variables and escape symbols. With single quotes, it does not interpret embedded variables and escape symbols.
  • 5. When the content needs to contain embedded quotes (single or double), no escape characters are needed. It inherently escapes single and double quotes, similar to the usage of q and qq.

Example

<?php
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. It starts with the <<<EOF marker and ends with the EOF marker. The ending marker must be at the beginning of the line, with no indentation or spaces, and must have a semicolon at the end.
  2. The starting and ending markers are the same. Commonly used uppercase markers like EOT, EOD, EOF are used, but not limited to those (JSON, HTML, etc., can also be used), as long as the starting and ending markers do not appear in the content.
  3. Variables located between the starting and ending markers can be parsed normally, but functions cannot. In heredoc, variables do not need to be concatenated with the dot (.) or comma (,) operator, as shown below:

Example

<?php
$name = "";
$a = <<<EOF
 "abc"$name
 "123"
EOF;
// The ending must be on its own line with no spaces before or after
echo $a;
?>

← Python3 Func RangeCss3 Pr Rotation Point β†’