Implement new closing tag =?> for keeping trailing newline #8708
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
The newline characters are all eaten by their preceding closing tags, so the output would be like this.
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.
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
results like this.
Of course, it can also be used in conjunction with non-shortened opening tags.
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.
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