Skip to content

Implement new closing tag =?> for keeping trailing newline #8708

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from

Conversation

sj-i
Copy link
Contributor

@sj-i sj-i commented Jun 5, 2022

Introduction

PHP currently automatically removes the newline character immediately following the closing tag "?>".

A more correct description of this would be that PHP's closing tag is "?>" with an optional trailing newline character.

For example, see the code below.

- <?= 1 ?>
- <?= 2 ?>
- <?= 3 ?>

The newline characters are all eaten by their preceding closing tags, so the output would be like this.

- 1- 2- 3

This behavior was adopted around the time of PHP3 because it was convenient at several points when using PHP for templating HTML.

However, it has long been known that this behavior can be inconvenient in some cases, such as when PHP is used for templating plain texts, where the newline character should appear exactly as it does in the template.

In the current behavior, adding an explicit newline character is a workaround for this.

- <?= 1 . "\n" ?>
- <?= 2 . "\n" ?>
- <?= 3 . "\n" ?>

This way, when the output block is moved to a location other than the end of a line by editing the template later, the extra newline character must be removed each time.

Proposal

Add "=?>" as a new PHP closing tag with no optional trailing newline character. It behaves exactly the same as the existing closing tag, except that it does not erase the trailing newline character. This allows developers to choose to leave newline characters in the template as they appear, and make the behavior of output blocks position-independent.

So the next code

- <?= 1 =?>
- <?= 2 =?>
- <?= 3 =?>

results like this.

- 1
- 2
- 3

Of course, it can also be used in conjunction with non-shortened opening tags.

- <?php echo 1 =?>
- <?php echo 2 =?>
- <?php echo 3 =?>

Note that in some cases it may be more appropriate to use an existing closing tag. When control statements are used in a template, it is probably preferable in many cases to start the control statement at the beginning of the line and terminate it with an existing closing tag, thus erasing the line containing the control statement completely from the output. The output of the following code is the same as the above examples.

<?php foreach([1, 2, 3] as $item): ?>
- <?= $item =?>
<?php endforeach ?>

This addition to the language requires only a one-line modification to the lexer and does not break backward compatibility.

I understand that some people believe that advanced templating should be attempted in userland, as there are no restrictions on release cycles, so faster development iterations can be done. However, I think the benefits of this small modification would be large enough, so this time I would like to propose a language-side modification. All projects that use bare PHP for their templates for whatever reason could benefit from this addition.

This change requires an RFC. I will request the wiki karma later in the internals ML if the reaction from the community is not bad.

References

@sj-i sj-i changed the title Implement closing tag keeping trailing newline Implement new closing tag =?> for keeping trailing newline Jun 5, 2022
@iluuu1994 iluuu1994 added the RFC label Jun 5, 2022
@sj-i
Copy link
Contributor Author

sj-i commented Jun 6, 2022

I asked for opinions on internals and reddit, and to put it mildly, it looks like it won't get 2/3 in favor, so I'm closing it.

Thanks for taking look at this!

@sj-i sj-i closed this Jun 6, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants