Re: Consider only ignoring newlines for final ?> in a file
| From: | Andrea Faulds | Date: | Thu, 07 Sep 2017 13:43:09 +0000 |
| Subject: | Re: Consider only ignoring newlines for final ?> in a file | ||
| References: | 1 2 3 4 | Groups: | php.internals |
| Request: | Send a blank email to internals+get-100442@lists.php.net to get a copy of this message | ||
Hi,
Nikita Popov wrote:
On Thu, Sep 7, 2017 at 2:43 PM, Andrea Faulds <ajf@ajf.me> wrote:Ah. See, it's actually that kind of code that is my problem. A practical example would be: <table>Hi Nikita, Nikita Popov wrote:I'm referring to code like <ul> <?php foreach ($data as $value): ?>It also goes the other way. Whether you want to drop the newline after ?> depends (roughly) on whether the code is control flow (drop) or trailing output (don't drop). If the newline is not dropped anymore it doesn't mean that the output will look nice, it's just going to be broken in a different way.I understand that it should be dropped for “control flow” code (maybe not the best term, I misunderstood what you meant at first). That's why I suggest ignoring the following newline only for the ?> at the end of the file, because I can't think of another place where you would have a ?> and *not* intend output immediately after it. So I'm not sure I understand your objection, from that standpoint. Did I miss something? Regards.<li><?= $value ?></li><?php endforeach; ?> </ul> Currently this would produce the output <ul><li>Foo</li> <li>Bar</li></ul> Without the trailing newline elision it would produce <ul><li>Foo</li> <li>Bar</li></ul> I always assumed that this is the reason why we do this in the first place.
<?php foreach($rows as $row): ?>
<tr>
<?php foreach ($row as $column): ?>
<td><?=htmlspecialchars($column)?></td>
<?php endforeach; ?>
</tr>
<?php endforeach; ?>
</table>
which currently produces:
<table>
<tr>
<td>foo</td>
<td>bar</td>
</tr>
<tr>
<td>baz</td>
<td>qux</td>
</tr>
</table>
The doubled-up indentation from missing newlines makes it into a mess. And this is even worse in practice when you have more nested control flow. Extra newlines would be fine here, but missing newlines aren't.
Thanks.
--
Andrea Faulds
https://ajf.me/