PHP Heredoc: How and When to Use it?

Problem:

Though you use double quotes(“”) or single quotes(‘’) to specify strings almost exclusively, but, you might have heard that in some situations to handle a string using heredoc could be more useful than the previous two syntaxes. So, you want to learn how to use it and when to use it.

Solution:

In the following discussion, you’ll learn about heredoc which is overlooked by many developers but might be very useful in situations.

What is PHP heredoc

There are four ways you can specify a string in PHP. Two of the most commonly used methods are double quote(“”) and single quote(‘’). Heredoc is just another way to specify a string.

 

PHP heredoc syntax

The following line shows a complete heredoc syntax.
<<<EOD
Your string goes here.
EOD;

Rules of writing a heredoc syntax-

  • In the heredoc syntax, there must be at least 3 lines.
  • In the first line, just mention three less than symbol(<<<) and a name of your choice. The name must be starts with a letter or an underscore. Then it might be any letter or numbers·
  • The string you want to specified must be starts from the second line and goes as long as it requires.
  • In the last line, you’ll again mention the name that you defined in the first line. It express the end of that heredoc syntax. Other than the heredoc name and semicolon, there should not be anything in this line.

 

PHP heredoc example

Now, see an example of heredoc below-

<pre>
<?php
$web_dev = <<<EOD
Learn web development at Schools of web.
Courses-
1. HTML5 2. CSS3 3. PHP
4. MySQL 5. JavaScript 6. jQuery
EOD;
echo $web_dev;
?>
</pre>

[wpdm_file id=163]

Output:
Learn web development at Schools of web.
Courses-
1. HTML5 2. CSS3 3. PHP
4. MySQL 5. JavaScript 6. jQuery

Note: For HTML <pre> tag the line breaks appear ion the output.

 

When to use heredoc?

(1) When you need to create/display lots of texts, heredoc is a good choose. You already have seen an example above.

(2) When you have a string that has lots of quotes to be escaped, then, heredoc is a very useful choice. Heredoc doesn’t require escaping quotes. One of the similar situation comes when you write an HTML email in PHP. See the example below-

<?php
$title = "Email Title";
$content = "Email content here";
$email = <<<EMAIL
<div id="outer">
    <div id="left">
        <h1>{$title}</h1>
        <span>{$content}</span>
    </div>
    <div id="right">Table of Content</div>
</div>
EMAIL;
echo $email;
?>

[wpdm_file id=164]

Instead of the heredoc, if you’d use double quotes you’d have to escape the double quotes like below-

<?php
$title = "Email Title";
$content = "Email content here";
$email = "
<div id=\"outer\">
    <div id=\"left\">
        <h1>{$title}</h1>
        <span>{$content}</span>
    </div>
    <div id=\"right\">Table of Content</div>
</div>
";
echo $email;
?>

[wpdm_file id=165]

Which one seems easier to you?

Caution: Heredoc is just another way to create strings. Don’t write another statement inside this. For example, when writing a long text inside heredoc, you might tend to write conditional statements or other statements which is required on that position. But, you can’t do that. See the example below to understand what it means-

<?php
$title = "Email Title";
$content = "Email content here";
$email = <<<EMAIL
<div id="outer">
    <div id="left">
        if(!empty($title))
            <h1>{$title}</h1>
        <span>{$content}</span>
    </div>
    <div id="right">Table of Content</div>
</div>
EMAIL;
echo $email;
?>

You can’t write the if statement which is in line #7. As, it is inside the heredoc.