# 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.

- Published: 2019-01-06
- Category: Languages
- Tags: PHP
- Reading time: 3 min read
- Source: https://www.muhammetsafak.com.tr/en/blog/php-7-3-flexible-heredoc-and-list-assignments/
- Language: en-US
- Author: Muhammet Şafak

---
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 the improvements to 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
<?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
<?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 twelve 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
<?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](/en/blog/php-7-1-nullable-types-and-void-return-type/) introduced the short bracket syntax as a replacement for `list()`: `[$a, $b] = $array`. That was a welcome change. Skipping elements with `list(, $second) = $array` has worked in both syntaxes from day one. What PHP 7.3 adds to the pair is reference assignment (`[&$first, $second] = $array`).

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

```php
<?php

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

// The short syntax has worked the same way since PHP 7.1:
[, $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 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](/en/blog/php-72-and-modern-php-habits/), 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.
