By using this site you agree to the use of cookies by Brugbart and our partners.

Learn more

Using PHP Heredoc Syntax

PHP heredoc have a couple of uses at least, learn about them in this PHP tutorial.

Edited: 2014-12-28 03:56

The main purpose of PHP heredoc syntax for strings, is to avoid problems with quotation in your strings, by no longer needing to escape your quotes – you will however still need to escape the doller sign, as it is used for variables. The choice whether to use the here document syntax or normal double and single quotes for your strings, is mostly about personal preference.

Using heredocs in PHP can be useful to avoid common quotation problems with strings that would otherwise have to use either single quotes or double quotes – especially if the string contains both. A string using heredoc syntax also accepts variables, just like a double quoted string would.

How to use heredoc in php

Using heredoc in php is done by first writing 3 less than signs, immediately followed by an end delimiter – this delimeter (sequence of characters) is used to identify the end of the string – that is done by repeating the delimiter at the end of the string.

  echo <<<END
This is a heredoc string, with "double" and 'single' quotation marks that includes a $variable

New lines are also preserved!
END;

Creating valid end identifiers

An end identifier is the part written after the three less than signs "<<<" – this part tells PHP what to use as the end identifier (the part that decides when to end the string.) An end identifier must consist of alphanumeric characters and underscores only, and it can not begin with a digit character.

  $sheredoc = <<<MY_END_ID
This massive string with line breaks, html, and all kinds of stuff, also contains both single "'" and double """ quotes.
This is written on line 2, have all the fun you want with it!
MY_END_ID;

As it has been demonstrated in the first example, you can also assign something to a variable for later use trough the use of PHP heredoc – it would however rarely, if at all be of any use, as you would likely just save it to a static file and load it when required.

See also: Reading Files in PHP

It is also important that the end identifier is not indented in any way – do not place tabs or spaces in front of the end delimiter!

  echo <<<MY_END_ID
This heredoc string has had its end identifying delimiter incorrectly indented. Do not do this in your own code!
     MY_END_ID;

Also, do not place anything after the simicolon on the same line as the end delimiter.

When to use php heredoc syntax

Aside from solving problems with string quotation, it is hard to think of other uses of heredoc. It will also somewhat improve the readability and maintainability of code, as you do not have to deal with long and messy string concatenation.

PHP heredoc can significantly cut down the development time of a project, but the time you can save is also directly connected with your coding speed – again, it will mostly be down to personal preference what to use.