PHP here document (heredoc) examples

PHP here doc FAQ: Can you share some examples of the PHP "here" doc (heredoc) syntax?

"Here" documents in many languages are provided as a way to address the following problems:

  1. You already know what a "here" document is, and you'd like to be able to use this functionality in PHP.
  2. You want to be able to print a long string of text in a PHP script, or in a PHP function.
  3. You want to be able to assign a long string of text to a PHP variable.

In this blog post I will demonstrate a number of different ways to use the PHP here document (heredoc) syntax to solve a variety of common problems.

Here are several different solutions to common problems involving here documents in PHP. As you'll see, each problem centers around the need to work with a long string, either printing that string, or assigning it to a variable.

PHP heredoc Solution 1: Printing a long sequence of HTML text in a PHP script

This is probably the most simple example of using a PHP here document. In this example, you just want to print a block of text from within your PHP code. Usually you'll be making some type of calculations, and then printing the block of text depending on the result of those calculations, but in this case I'm just going to show you how to print the block of text:

<?php

# assume that your conditional code is up here ...

# and now you want to print the block of text:

print <<< END
<p>
Four score and seven years ago<br/>
our fathers set onto this continent<br/>
(and so on ...)<br/>
</p>
END;

# more logic down here...

?>

Here are a few important points to remember about PHP here documents:

  1. They begin with the print <<< END syntax.
  2. The here document ends with this line: END;. To be clear, that's the word END, followed by a semi-colon. This closing string needs to begin in column one; there can't be any leading spaces.
  3. The word END can be any string you want it to be. I just chose END because it makes the most sense to me. I'll use a different string in my next example.

PHP heredoc Solution 2: Printing a long sequence of HTML text in a PHP function

In this example, let's assume that you need to be able to print a multi-line block of HTML text from a PHP function. To solve this problem just write your PHP heredoc function like this:

<?php

function print_footer()
{
  print <<< FOOBAR
<div id ="footer">
printed by alvin alexander,
<br/>devdaily.com
</div>
FOOBAR;
}

?>

and then call it later in your script, like this:

print_footer();

Calling this function results in the following output:

<div id ="footer">
printed by alvin alexander,
<br/>devdaily.com
</div>

PHP heredoc Solution 3: Assigning a long sequence of text to a PHP variable

Another way to use the PHP heredoc syntax is to assign a long string to a variable. You do this by combining the assignment operator with the heredoc syntax, like this:

<?php

# assign the variable
$var = <<< LINCOLN
Four score and seven years ago
our fathers set onto this continent
(and so on ...)
LINCOLN;

?>

Later on you can use the variable however you'd like. Here's one example, where I just print out the value of the variable, which is of course the text I assigned to it above:

# do whatever else you need to do in your program,
# and then print the variable here:
print $var;