Hudzilla.org - the homepage of Paul Hudson
Contents > Introducing PHP > How PHP is written Wish List | Report Bug | About Me ]

2.6.3     Heredoc

This is NOT the latest copy of this book; click here for the latest version.

In order to allow people to easily write large amounts of text from within PHP, but without the need to constantly escape things, heredoc syntax was developed. Heredoc might be a little tricky to understand at first, but it is actually a big help. Put simply, it allows you to define your own string limiter so that you can make it something other than a double or single quote. So, for example, we could use the string "EOT" (end of text) for our delimiter, meaning that we can use double quotes and single quotes freely within the body of the text - the string only ends when we type EOT.

It is a little more complicated than that in practice, but not much - the string delimiter needs to be by itself on a line, in the very first column. That is, you cannot add spacing or tabs around it.

Take a look at this example:

<?php
$mystring
= <<<EOT
    This is some PHP text.
    It is completely free
    I can use "double quotes"
    and 'single quotes',
    plus $variables too, which will
    be properly converted to their values,
    you can even type EOT, as long as it
    is not alone on a line, like this:
EOT;
?>

There are several key things to note about heredoc, and the example above:

  • You can use anything you like; "EOT" is just an example

  • You need to use <<< before the delimiter to tell PHP you want to enter heredoc mode

  • Variable substitution is used in PHP, which means you do need to escape dollar symbols - if you do not, PHP will attempt variable replacement.

  • You can use your delimiter anywhere in the text, but not in the first column of a new line

  • At the end of the string, just type the delimiter with no spaces around it, followed by a semi-colon to end the statement

Without heredoc syntax, complicated string assignments can quickly become very messy. Heredoc is not used all that often in the wild - very often you will wish it were used more, because too many scripts you will come across have messy code as a result of not using heredoc!





<< 2.6.2 Escape sequences   2.6.4 Brief introduction to variable types >>
Table of Contents
Want to see this stuff in print? PHP in a Nutshell takes the core topics covered here, adds in thousands of edits from the editorial team and myself, and combines them to make an unbeatable reference for PHP programmers at all levels.



My latest book has hundreds more tips on how to use PHP, Apache, and MySQL, plus Perl, Python, shell scripts, performance tuning, and more!



Top-right shadow
 
Bottom-left shadow Bottom shadow

Comments from other readers
Safiya Dogara Bashir/sdbashir2001@yahoo.com - 30 Aug 2008

I noticed the same thing as CGS, so I looked at the source code for these two examples. The only difference is that the line "This is some PHP text." is proceeded by white space in the first example, and none in the second example.

According to that link to php.net (another thanks for that link), the only rules to watch out for are on the ending "EOT;" line - no whitespace can exist before the delimiter, nor can there be any other character aside from the semicolon, and the character to end the previous line must be "a newline as defined by the operating system" e.g. /n or /r.

(I tried to post this once but it didn't show up after a refresh of my browser - sorry if it's duplicated)

tiwiex - 30 Aug 2008

I did not see any difference between the two samples above. i.e.
$mystring = <<<EOT
This is some PHP text.
EOT;

The example above will return an error, but

$mystring = <<<EOT
This is some PHP text.
EOT;

can u please point it out. anyway apart from the ending EOT i don't think spaces matter or not on the starting EOT.

A PHP User - 30 Aug 2008

In the first example... there is a space after text.

$mystring = <<<EOT
This is some PHP text. <---
EOT;

Which doesnt show in this little editor but can be
seen when the text is highlighted.

Just thought I'd point that out. :)
p.s. Im enjoying your book so far.. Thanks.

Kyle - 30 Aug 2008

I noticed the same thing as CGS, so I looked at the source code for these two examples. The only difference is that the line "This is some PHP text." is proceeded by white space in the first example, and none in the second example.

According to that link to php.net (another thanks for that link), the only rules to watch out for are on the ending "EOT;" line - no whitespace can exist before the delimiter, nor can there be any other character aside from the semicolon, and the character to end the previous line must be "a newline as defined by the operating system" e.g. /n or /r.

(I tried to post this once but it didn't show up after a refresh of my browser - sorry if it's duplicated)

A PHP User - 30 Aug 2008

Maybe it's just my browser, but your two examples look exactly the same. Thanks for the link though.

CGS

A PHP User - 30 Aug 2008

Make a note, that in heredoc you HAVE to be careful on few things:

$mystring = <<<EOT
This is some PHP text.
EOT;

The example above will return an error, but

$mystring = <<<EOT
This is some PHP text.
EOT;

this will not...

Read carefully the information about heredoc here:
http://php.net/manual/en/language.types.string.php#language.types.string.syntax.heredoc



Add comment
Please note that by posting a comment here you are committing it to the public domain. This is important so that others can make use of your code themselves, and also so that I can incorporate helpful notes directly into the main text. Comments are limited to 2000 characters in length.

If you are reporting an error in the content, please tell me directly.

Your name/email address:
Your comment:
 
Now, in order to verify that you're a real person, please answer this simple question: what is one plus two?
The answer is:
(please write in
numbers, eg 19)


Top-right shadow
 
Bottom-left shadow Bottom shadow