|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
[2015-11-27 06:35 UTC] php at rossw dot net
Description:
------------
A quoted string containing ?> is treated correctly inside an active code block, but causes errors due to being handled differently if the line is commented out.
I can see how it could be argued that ?> should not appear, but there are times you might actually WANT to print it. Simply commenting out the line should not materially alter the way the (otherwise perfectly healthy) script runs.
Test script:
---------------
<?php
printf("This is a test\n");
if(1 > 0)
{
// printf("?> Debug code\n");
printf("Got here\n");
}
printf("Finished\n");
?>
Expected result:
----------------
php is seeing the "?> Debug code" as quoted text, and handles it correctly while the line is not commented. As soon as the line is commented, php is seeing the quoted ?> as the end of the code and errors.
PatchesPull Requests
Pull requests:
HistoryAllCommentsChangesGit/SVN commits
|
|||||||||||||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Thu Nov 13 21:00:02 2025 UTC |
That behaviour is explicitly defined here: <ST_IN_SCRIPTING>"#"|"//" { while (YYCURSOR < YYLIMIT) { switch (*YYCURSOR++) { case '\r': if (*YYCURSOR == '\n') { YYCURSOR++; } /* fall through */ case '\n': CG(zend_lineno)++; break; case '?': if (*YYCURSOR == '>') { YYCURSOR--; break; } /* fall through */ default: continue; } break; } yyleng = YYCURSOR - SCNG(yy_text); RETURN_TOKEN(T_COMMENT); } Removing the "case '?':" clause would break the following inline code: <?php echo "Hello, World!"; // my comment ?> Remainder Which should print: Hello, World! Remainder Everything on the same line after the comment including after the closing tag are commented out.I understand this request. PHP ought to know if this is a single-line execute, and handle accordingly. <?php echo "hello"; // comment ?> output and <?= echo "hello"; // comment ?> output should each run with output: hello output That said, the request here: <?php if (foo) { // ?> } } ?> Should not throw a syntax error. I have no idea what the consensus on this kind of issue is, however, (i've done) and it would be simple enough to track what line <?php, or <?, or <?= start on, and what line we see // ?>, and if they aren't the same, then ignore the ?> inside the comment. ie: if (*YYCURSOR == '>' && (CG(zend_opentag_lineno) == CG(zend_lineno))) {