# One-line heredoc for better syntax highlightning

- Thread: https://externals.io/message/118660
- Original archive: https://news-web.php.net/php.internals/118660
- Started by olleharstedt@gmail.com on 2022-09-19T19:04:07+00:00
- Messages: 3
- Reply by email to internals@lists.php.net
- More for agents: search with https://externals.io/search.md?q={query} · JSON API at https://externals.io/api · documentation at https://externals.io/llms.txt

---

## #118660 — olleharstedt@gmail.com — 2022-09-19T19:04:07+00:00

URL: https://externals.io/message/118660#118660

> I don't find that example particularly readable or convincing. It mushes
> together PHP and MySQL keywords too much.
>
> Once you add a WHERE clause that SQL query should be split across
> multiple multiple lines for readability anyway and even for that very
> simple example I personally would add a linebreak in front of the FROM:
>
> $query = <<<MySQL
> SELECT *
> FROM foo
> MySQL;
>
> Best regards
>Tim Düsterhus

That looks good in isolation, but consider the following short snippet:

class Foo
{
    public function bar()
    {
        $query = <<<MySQL
        SELECT * FROM baz
MySQL;
    }
}

As you can see, the heredoc breaks the reading flow completely. For
smaller SQL queries, using one line would be an improvement, I think.

Also consider that the one line will look better in your color
highlight editor. :)

Olle

---

## #118662 — tim@bastelstu.be — 2022-09-19T19:18:08+00:00

URL: https://externals.io/message/118660#118662 · In reply to: #118660

Hi


> That looks good in isolation, but consider the following short snippet:
> 
> class Foo
> {
>      public function bar()
>      {
>          $query = <<<MySQL
>          SELECT * FROM baz
> MySQL;
>      }
> }
> 
> As you can see, the heredoc breaks the reading flow completely. For

The closing marker of the heredoc may be indented as of PHP 7.3, so the 
appropriate comparison would be this:

<?php

class Foo
{
     public function __construct(
         private \PDO $dbConnection
     ) {}

     public function bar()
     {
         $query = <<<MySQL
             SELECT  *
             FROM    baz
             MySQL;
         $statement = $this->dbConnection->prepare($query);
         $statement->execute();
     }
}

> smaller SQL queries, using one line would be an improvement, I think.

A condition-less SELECT should be rare, a condition-less UPDATE or 
DELETE even rarer. So this "improvement" would only affect a very small 
number of statements in the first place and I disagree with that being 
an improvement even for that small number of statements, because it 
would be inconsistent with all the other larger statements which *are* 
spread across multiple lines.

> Also consider that the one line will look better in your color
> highlight editor. :)
> 

I believe that code should be readable no matter the editor it is viewed 
in. The suggestion fails at that.

Best regards
Tim Düsterhus

---

## #118663 — olleharstedt@gmail.com — 2022-09-19T19:23:08+00:00

URL: https://externals.io/message/118660#118663 · In reply to: #118662

2022-09-19 21:18 GMT+02:00, Tim Düsterhus <tim@bastelstu.be>:
> Hi
>
> 
> > […]
>
> The closing marker of the heredoc may be indented as of PHP 7.3, so the
> appropriate comparison would be this:
>
> <?php
>
> class Foo
> {
>      public function __construct(
>          private \PDO $dbConnection
>      ) {}
>
>      public function bar()
>      {
>          $query = <<<MySQL
>              SELECT  *
>              FROM    baz
>              MySQL;
>          $statement = $this->dbConnection->prepare($query);
>          $statement->execute();
>      }
> }

Pff, that's the second time today 3v4l gives me complete faulty
results in its syntax checker. Guess it's out of date. Thanks for
correcting me here.

Olle
