What is the heredoc syntax in PHP and how is it used?

Most of the time, programmers --especially those that write in PHP – forget or are oblivious to the fact that there are more than just two ways to deal with strings. The two popular ways are:

  • Using single quotes (' ')
<?php
$mystring = 'Using single Quotes';
?>

  • Using double quotes (" ").
<?php
$mystring = "Using double Quotes";
?>

In the two examples above, we declare string variables in PHP, but this can be done in other ways that can have unique advantages. Let’s look at one of those ways.

What is the heredoc syntax?

The heredoc syntax is a way to declare a string variable. The heredoc syntax takes at least three lines of your code and uses the special character <<< at the beginning.

Syntax

$nameOfVariable = <<< identifier 
// string
// string
// string
identifier;

identifier designates the beginning and end of the string, which must be at least 3 lines. identifier can be any word you specify.

Code

<?php
$edpresso = <<<justRandomSpecifier
Write interractive byte sized shots<br>
You can Write on:<br>
1. Web development<br>
2. Dev OPS<br>
3. Machine learning<br>
4. Datascience <br>
5. Networking etc.
justRandomSpecifier;
echo $edpresso;
?>

Explanation

  • The variable in the snippet $edpresso is a variable of type string that spans multiple lines.

  • The justRandomSpecifier beginning of the file indicator, which is also used at the end of the file, can be any word or combination of characters.

  • The <br> tag prints each line on a single line in the output.

DOs and DON’Ts of heredoc syntax usage

  • The variable must have at least three lines and should start with <<< and a beginning of file identifier in the first line. After the last line, there must be the same identifier again.

  • You can have HTML tags as part of the string, which will be interpreted as HTML elements of the browser without the need to escape special characters.

  • Don’t attempt to add a function or condition in the strings; this is an erroneous operation and it won’t execute the condition or function.

  • Use curly brackets {} to contain any other variable that you want to display the contents of as part of the strings.

Further examples

Below are two more examples. The first is one that shows how to format as if you were making a header and body text in an email. The second is an example of incorrect usage, where the formatting will come out incorrectly.

<?php
$title = "The title of my email";
$body = "This email will contain this";
$email = <<<heredocEmail
<div >
<div >
<h1>{$title}</h1>
<p>{$body}</p>
</div>
<div id="right">What we offer</div>
</div>
heredocEmail;
echo $email;
?>
<?php
$mytitle = "The title of my email";
$$body = "This email will contain this";
$email = <<< heredocEmail
<div id="outer">
<div id="left">
if(!empty($title))
<h1>{$title}</h1>
<p>{$body}</p>
</div>
<div id="right">What we offer</div>
</div>
heredocEmail;
echo $email;
?>

Try to use more of this syntax in your codes to better understand its usefulness. I mostly use heredoc syntax when I have lots of text to output that may contain special characters and the like.

Free Resources