php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #31787 Parse Error -- Comment of // slash slash fails to ignore script terminator
Submitted: 2005-02-01 00:36 UTC Modified: 2005-02-04 15:03 UTC
From: codeslinger at compsalot dot com Assigned:
Status: Not a bug Package: Scripting Engine problem
PHP Version: 4.3.10 OS: Linux
Private report: No CVE-ID: None
 [2005-02-01 00:36 UTC] codeslinger at compsalot dot com
Description:
------------
Here is a bizarre script parsing problem

The // comment operator is supposed to ignore everything on that line.  But if you happen to do something like

// fwrite($pipes[0], "<?php echo 'Hello World';  ?>" );

Then you start getting all kinds of strange parse errors.

I isolated the problem.  What is happening is that

// ?>
or even  // "?>"

is being interpeted as the script terminator.

Happily  /*  ?>  */ does work


Reproduce code:
---------------
<?php

// ?>
  echo "we get an error because it sees the ?> in the comment";

?>

# The above will fail, but this works

<?php

/*
?>
*/
  echo "the ?> is properly ignored";

?>



Expected result:
----------------
anything and everything following a // to be ignored


Actual result:
--------------
Warning: Unexpected character in input:  '\' (ASCII=92) state=1 in /foo/phpchild/parent1.php on line 29

Parse error: parse error, unexpected ')', expecting ',' or ';' in /foo/phpchild/parent1.php on line 29


The actual error depends on the specific code following the termination point.

This thing really had me baffeled for awhile...
I commented out several lines of code and started getting parse errors about missing parens and semicolons, etc.

Depending upon what I commented out, the error would change, which made it very hard to track down. Naturally I assumed I had made a mistake, mismatched quotes etc. Took me quite a bit of head banging to finally figure out what was going on.

=============
"Are you sure that you searched before you submitted your bug report?"

By the way....   You don't allow searching for specific characters such as //  and  ?>   this makes it very hard to check for duplicates of this bug....
   
I note that your auto-dup detector did search for // in fact it did a very impressive job of finding similar bugs.  But no dups.



Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2005-02-01 00:46 UTC] codeslinger at compsalot dot com
I doubt it makes a difference, but this is the CLI version of php.
 [2005-02-01 09:24 UTC] derick@php.net
Thank you for taking the time to write to us, but this is not
a bug. Please double-check the documentation available at
http://www.php.net/manual/ and the instructions on how to report
a bug at http://bugs.php.net/how-to-report.php

Please read the docs on html containment - this is expected.
 [2005-02-03 18:49 UTC] codeslinger at compsalot dot com
Thank you for your response.  But I wanted some further discussion on this.

I spent a *lot* of time banging my head against the wall trying to track down a nonsensical error message.

I took the totally legitimate statment of
fwrite($fp, "<?php echo 'Hello World';  ?>" );

And commented it out with //.  And instead of ignoring the comment, php decided to interpet it as a script close tag.

This bogus parsing behavior lead to major frustration and confusion; and wasted time.

Now my understanding of // is that everything up to the end of the line is supposed to be ignored.  Not only is the ?> within a comment, but it is also contained inside of quotes...  

Why is the parser spinning it's wheels parsing the contents of a comment, when all it needs to be doing is a skip to end of line?

------

Per your suggestion, I did a search for "html containment"  I did not find anything.  I did find and read chapter 71, but saw nothing applicable.  

I have actually read quite a bit of the manual, but as you know it is huge and navigation can be tricky, if you could give me a more specific link I would appreciate it.

----------

Please understand that I LOVE PHP and I am very very greatful for all the hard work that people have put in.  I am not intending to be overly critical.
 [2005-02-03 18:53 UTC] sniper@php.net
This is not discussion forum. Discuss on the mailing lists.
Documentation can be found at:

http://www.php.net/manual/en/language.basic-syntax.comments.php

Read the last paragraph on that page.

 [2005-02-04 07:14 UTC] codeslinger at compsalot dot com
okay, thanks.

But I still think it's strange for the parser to be wasting time, parsing code inside of a comment, when all it needs to be doing is looking for the end of line.
 [2005-02-04 15:03 UTC] codeslinger at compsalot dot com
for anyone else reading this bug...  
the answer is because of code blocks e.g. embeded code.

Consider:
<?php echo "foo"; //a comment ?> <i>html here</i><?php echo "more foo"; ?>

if the parser just looked for the end of line it would skip valid stuff.
 
PHP Copyright © 2001-2025 The PHP Group
All rights reserved.
Last updated: Fri Jul 25 00:00:02 2025 UTC