php 7.3 - Flexible heredoc & nowdoc Syntax

php logo

Although we've still got a while to go before we see php 7.3, since we're still 4 days away from the release of php 7.2, I've already seen one new feature that looks like it will make its way into 7.3 that I am excited about. This feature is more flexible syntax for heredoc and nowdoc statements. Here is the official rfc if you'd like to read it.

What are heredocs and nowdocs?

Heredocs and nowdocs are the easiest and cleanest ways in php to use and format multiline strings.

Instead of:

$html = '<ul>';
$html .= '<li>item 1</li>';
$html .= '<li>item 2</li>';
$html .= '<li>item 3</li>';
$html = '</ul>';

We can use:

$html = <<<html
<ul>
    <li>item 1</li>
    <li>item 2</li>
    <li>item 3</li>
</ul>
html

The only difference between a heredoc and a nowdoc is that a heredoc performs string interpolation, turning your variables into the string they represent, while a nowdoc does not.

//heredoc example
$name = 'Todd Eidson';
$text = <<<txt
My name is $name
txt

echo $text;
// outputs 'My name is Todd Eidson'
//nowdoc example
$name = 'Todd Eidson';
// notice the single quotes around the marker 
$text = <<<'txt'
My name is $name
txt

echo $text;
// outputs 'My name is $name'

One great thing about using a heredoc/nowdoc within a good IDE (like my favorite, PhpStorm) is that php allows you to pick your own heredoc marker, and PhpStorm is smart enough to give you syntax highlighting inside the heredoc based on the marker used. For example if you use sql or HTML for your marker name, your heredoc string will use the code highlighting of the marker, making mistakes easy to spot.

Caveat

The only caveat (for now) when using a heredoc/nowdoc is that the end of statment marker must be the first and only thing on the line. This leads to left aligned text in the middle of a function or method that can be quite jarring when quickly scanning code, much like coming to a stop sign in the middle of an interstate, your brain has to slow down for a second to figure out why your indentation is not being properly followed.

Like so:

class MyClass
{
    public function MyFunction($variable)
    {
        if ($variable) {
            $html = <<<html
Why is this code against the left margin?
It would be really nice if it was
properly indented.             
html
        }
        return $html;
    }
}

The Changes

In php 7.3 we will be able to indent the end of heredoc marker to improve readability. Any whitespace before ending marker will also be stripped off the front of the each line in the multiline string. Any line that is not indented to at least the level of the ending marker will throw an error.

An example:

class MyClass
{
    public function MyFunction($variable)
    {
        if ($variable) {
            $html = <<<html
                Isn't this so much easier to read?
                It is really nice that it is
                properly indented.
                And so much easier to read!             
                html
        }
        return $html;
    }
}

The other chage is that the ending marker does not have to be followed by a new line. I can't remember a time at which I wished for this ability, so I'm not nearly as excited about this as I am the ability to indent the ending marker, but I'm sure some people will get some use out of it. I guess it could be useful if using multiple heredocs in a function call or an array.

Like this:

// The old way:
myFunction(<<<txt
This is
the first parameter
txt
, <<<txt
This is
the second parameter
txt
);


// The new way
myFunction(<<<txt
This is
the first parameter
txt, <<<txt
This is
the second parameter
txt
);

Conclusion

I'm really looking forward to when I can get switched over to 7.3 (or 8, whichever comes first) so I can take advantage of this new feature and clean up all my heredocs. I do recommend reading the rfc as there are some nuances concerning marker names and a raised backwards incompatability issue that I don't go over in this post.

What are your thoughts on this change? Yay, nay, or meh?

If you liked this post, you can subscribe to the rss feed or follow me @ToddEidson on Twitter to be notified of future blog posts.

Date Published: 26 November, 2017

Tags: php php 7.3

About Todd

Full stack application developer. Life-long learner. Pragmatic programmer. Believer in clean coding. Proponent for extensible and reusable code. Hobbies include (very) amateur photography, collecting old jazz records and going to live music performances.

North Central Ohio, US
toddeidson[dot]info

Obligatory Disclaimer

All opinions are my own, probably wrong, and subject to change without notice.

© 2017-2019 Todd Eidson. All content is licensed under the Creative Commons Attribution-ShareAlike 4.0 International License.

Hosted on linode