Ad space (banner)
๐Ÿ˜PHP Lessons
Lesson 46 / 68

Multi-Line Strings (Heredoc)

In this lesson you will learn PHP's heredoc syntax for multi-line strings so you can assemble readable messages. This is for anyone searching for "PHP heredoc how to use".

The <<<EOT ... EOT; form (a heredoc) lets you write a multi-line string with the line breaks included. Variables can be embedded directly. It is handy for laying out an email body or a long explanation readably.

The sample code builds a multi-line message with {$name} embedded, between <<<EOT and EOT;. The EOT identifier can be any name you like, as long as the opening and closing ones match exactly.

A common stumbling block for beginners is the closing identifier (EOT;). Extra whitespace at the start of the line can cause an error, and the rules around the identifier are strict, so copying a working example is a good idea while you get used to it.

Because line breaks and indentation are preserved exactly, long text such as an email body is much easier to assemble. Multi-line messages with variables embedded read far better as a heredoc.

๐Ÿ“– Reference code
โœ๏ธ Your code
Type your code, then press "Run"

๐Ÿงช This site can't compile or run PHP directly, so it checks on the spot whether what you typed matches the reference code (scoring happens entirely in your browser โ€” nothing is sent anywhere).

Ad space (banner)
Ad space (in-article)