From EditPlus Wiki
Revision as of 19:56, 6 October 2005 by (Talk) (Add bit about quoting the limit string)

Jump to: navigation, search

Heredoc syntax provides a means to delimit a string without using single or double quotes. This can be useful in situations with strings that contain many single or double quotes or that break across multiple lines.

EditPlus does not natively support Syntax Highlighting for heredoc strings, however you can modify an .stx file to emulate string highlighting for heredoc strings.

PHP Syntax

In PHP, <<< (three left angle brackets) is the heredoc operator, denoting the beginning of the string. This operator is followed immediately by a custom identifier, which is then used to close the heredoc string. The closing identifier must begin in the first column of the line, containing no other characters except a semicolon (;). There can be no spaces/tabs after or before the identifier or the semicolon.

The following is an example of heredoc string usage:

$str = <<<EOF
Example of string
spanning multiple lines
using heredoc syntax.
print $str;

Bash Syntax

In Bash, a here document is a special-purpose code block. It uses a form of I/O redirection to feed a command list to an interactive program or a command, such as ftp, cat, or the ex text editor. A limit string delineates (frames) the command list. The special symbol << designates the limit string. This has the effect of redirecting the output of a file into the stdin of the program or command.

The here document looks like this:

interactive-program <<LimitString
command #1
command #2

By default bash will interpolate the content of the block like it would in a double-quoted string. If you want to avoid this (for example if your here document contains a script you want to output verbatim) you can put single quotes around your limit string.

user@host> var="hello"
user@host> cat <<LimitString
user@host>  echo $var
user@host>  LimitString
echo hello
user@host> cat <<'LimitString'
user@host>  echo $var
user@host>  LimitString
echo $var

External Links

PHP Heredoc Reference

Advanced Bash-Scripting Guide: Chapter 17. Here Documents