Skip to content

Commit c9acc90

Browse files
committed
Support <?php followed by EOF
This is an annoying edge-case for canonicalization.
1 parent ebfa056 commit c9acc90

File tree

5 files changed

+43
-0
lines changed

5 files changed

+43
-0
lines changed

UPGRADING

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,10 @@ PHP 7.4 UPGRADE NOTES
3434
. Passing the result of a (non-reference) list() assignment by reference is
3535
consistently disallowed now. Previously this worked if the right hand side
3636
was a simple (CV) variable and did not occur as part of the list().
37+
. `<?php` at the end of the file (without trailing newline) will now be
38+
interpreted as an opening PHP tag. Previously it was interpreted either as
39+
`<? php` and resulted in a syntax error (with short_open_tag=1) or was
40+
interpreted as a literal `<?php` string (with short_open_tag=0).
3741

3842
- BCMath:
3943
. BCMath functions will now warn if a non well-formed number is passed, such

Zend/tests/php_tag_only.inc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
<?php

Zend/tests/php_tag_only.phpt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
--TEST--
2+
File with just a <?php tag should be valid
3+
--FILE_EXTERNAL--
4+
php_tag_only.inc
5+
--EXPECT--

Zend/zend_language_scanner.l

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2031,6 +2031,20 @@ string:
20312031
RETURN_OR_SKIP_TOKEN(T_OPEN_TAG);
20322032
}
20332033

2034+
<INITIAL>"<?php" {
2035+
/* Allow <?php followed by end of file. */
2036+
if (YYCURSOR == YYLIMIT) {
2037+
BEGIN(ST_IN_SCRIPTING);
2038+
RETURN_OR_SKIP_TOKEN(T_OPEN_TAG);
2039+
}
2040+
/* Degenerate case: <?phpX is interpreted as <? phpX with short tags. */
2041+
if (CG(short_tags)) {
2042+
yyless(2);
2043+
BEGIN(ST_IN_SCRIPTING);
2044+
RETURN_OR_SKIP_TOKEN(T_OPEN_TAG);
2045+
}
2046+
goto inline_char_handler;
2047+
}
20342048

20352049
<INITIAL>"<?" {
20362050
if (CG(short_tags)) {

ext/tokenizer/tests/php_tag_only.phpt

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
--TEST--
2+
Tokenization of only the <?php tag
3+
--FILE--
4+
<?php
5+
6+
foreach (token_get_all("<?php") as $token) {
7+
echo token_name($token[0]), "\n";
8+
}
9+
echo "\n";
10+
foreach (token_get_all("Foobar<?php") as $token) {
11+
echo token_name($token[0]), "\n";
12+
}
13+
14+
?>
15+
--EXPECT--
16+
T_OPEN_TAG
17+
18+
T_INLINE_HTML
19+
T_OPEN_TAG

0 commit comments

Comments
 (0)