Skip to content
Muhammet Şafak
tr
Languages 4 min read

PHP 7.3: Flexible Heredoc and List Assignments

A hands-on look at PHP 7.3's flexible heredoc syntax and list assignment improvements, and how they contribute to better code readability.


I always look forward to major PHP releases — 7.0 and 7.1 were genuine turning points for the language. PHP 7.3, on the other hand, got labeled a “minor release” right out of the gate. But after spending some time with it, I’ve come to appreciate that small language additions can have a surprisingly large long-term impact on code readability. Today I want to focus on two specific changes: the flexible heredoc syntax and short syntax support for list assignments.

Neither of these features is flashy. Show them to someone and they’ll say “we waited for this?” But things that reduce repetitive friction while writing code are what make the real difference at the end of the day.

Flexible Heredoc Syntax

Heredoc is PHP’s syntax for defining multi-line text blocks. In PHP 7.2 and earlier, there was one strict rule: the closing label had to sit at the very beginning of the line, with no indentation whatsoever. If your code was indented — which it almost always is — this rule turned into a constant visual annoyance.

Here’s what the old situation looked like:

<?php

class Mailer
{
    public function buildBody(string $name): string
    {
        return <<<EOT
Merhaba $name,

Mesajınız alındı.
EOT;
// Closing label must be flush-left — outside the class indentation
    }
}

Reading this in an IDE always bothered me. You’re working inside an indented class, and then the closing label suddenly jumps all the way to the left margin — visually jarring.

With PHP 7.3, the closing label can now be indented:

<?php

class Mailer
{
    public function buildBody(string $name): string
    {
        return <<<EOT
            Merhaba $name,

            Mesajınız alındı.
            EOT;
    }
}

The indentation of the closing label is automatically stripped from each line of content. So the eight spaces before EOT are subtracted from every line in the body. The resulting string is identical; the visual layout is far cleaner.

The same rule applies to nowdoc (the single-quoted variant with no variable interpolation):

<?php

$sql = <<<'SQL'
    SELECT id, name
    FROM users
    WHERE active = 1
    SQL;

When writing SQL queries, template strings, or long JSON fragments this way, I no longer have to sacrifice indentation for correctness.

The indentation trap: tabs vs. spaces

There is one thing to watch out for: the indentation characters used in the closing label and in the content lines must match. If one uses tabs and the other uses spaces, PHP throws a ParseError. This is a real gotcha in teams where editor settings vary, or when Git has silently converted tabs to spaces. Keeping editor settings consistent across the project prevents the problem entirely.

Short Syntax for List Assignments

PHP 7.1 introduced the short bracket syntax as a replacement for list(): [$a, $b] = $array. That was a welcome change. However, the same release revealed an inconsistency: skipping elements with list(, $second) = $array did not work with the new short syntax. PHP 7.3 closes that gap.

Consider parsing a CSV row where we don’t care about the first column:

<?php

// This worked in PHP 7.1:
list(, $isim, $email) = explode(',', $satir);

// Short syntax (errored in PHP 7.1, works in 7.3):
[, $isim, $email] = explode(',', $satir);

It looks small, but in practice making list() and [] follow the same rules means less mental overhead and one fewer inconsistency to keep in mind.

This consistency also pays off when processing multi-dimensional data. If a database query returns rows and you only need specific columns, you can now use element-skipping syntax in nested assignments. Every time I use it I find myself wondering why it took this long.

Why These Changes Matter

When I evaluate a language feature, I ask myself: “How often will I actually use this?” There are very few substantial projects that never touch heredoc. Multi-dimensional data parsing shows up in almost every application. That’s why I think the “minor release” label does PHP 7.3 a disservice — it’s reducing friction in exactly the right places.

That said, are these changes enough of a reason to upgrade on their own? Not in isolation. But if your project is already planning a migration from PHP 7.2, these improvements are small wins that make the decision a little easier to justify.

Don’t underestimate PHP’s minor releases. Paradigm-shifting changes are rare; cumulative improvements that reduce friction are things you feel every single day.

Tags: #PHP
Share:

Comments

Sign in with your GitHub account to join the discussion. Comments are stored in GitHub Discussions.

Related Posts

Search the site

Start typing to search posts, projects and pages.

Esc to close Powered by Pagefind