php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Doc Bug #20814 reproducable, freaky parse error in 'here document' structure
Submitted: 2002-12-04 11:04 UTC Modified: 2003-01-21 01:30 UTC
From: kumar at chicagomodular dot com Assigned:
Status: Closed Package: Documentation problem
PHP Version: 4.4.0-dev OS: all
Private report: No CVE-ID: None
 [2002-12-04 11:04 UTC] kumar at chicagomodular dot com
I've tested this on PHP 4.2.3 for OSX, Open BSD, Linux...

If you leave any whitespace after the closing tag of a here 
document structure (not sure if that's what it is called, 
like in Perl) a parse error will occur but never for the 
correct line.  This seems like a bug to me because a semi-
colon should terminate the code allowing the programmer to 
put comments or whatever after the semi-colon.

Here is an example:

$variable = "variable";
$someVariable = <<<hereDocument
	<html>
	<body>
	<b>text text $variable
	testing here document</b>
	</body>
	</html>
hereDocument; // closing comment or just whitespace
echo $someVariable;

After the closing "hereDocument;" if you leave any 
whitespace (other than a carriage return) or text, a parse 
error is generated but the line number never leads you to 
the right place.  This can be very confusing if you are 
working on a large class (like I was) and you left a tab or 
space after a closing tag.  I'm not sure how consciously 
supported the here document structure is but this is worth 
addressing since here documents can be very useful for 
storing large variables containing HTML code with double 
and single quotes, for example.  While I'm submitting this 
bug, I might as well point

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2002-12-04 11:23 UTC] kumar at chicagomodular dot com
[quick on the trigger]... scratch that last sentence ... I 
was going to point out that the closing tag has to be flush 
left but I think that is not a bug.
 [2002-12-04 11:24 UTC] philip@php.net
To clarify a few points:

a) This is known and is both documented and is a current feature request:

http://bugs.php.net/bug.php?id=8685
http://www.php.net/language.types.string#language.types.string.syntax.heredoc

b) The error that's reported is odd, and looks something like this:

Parse error: parse error, unexpected $ in test.php on line 42

Where 42 is, for example, on the last line of test.php even when the error is elsewhere.  This error sorta makes sense in most cases (like a missing quote or closing brace) but IMHO not here.  Maybe someone can briefly explain what PHP is thinking.

c) Many have attempted to fix this and many more have reported it (a) but it appears to be almost impossible to fix and remains a feature request.  Again, it is documented with a big fat warning.

Am leaving as open as this mentions the strange error too (b), not just the known feature request.



 [2002-12-04 14:58 UTC] msopacua@php.net
To clarify the line mismatch:
AFAIK it's the same as a missing '}' in if/else. It will report on the __last__ expected '}', while the error will intuitively start on the __first__ unclosed '}'.

The '$' is 'end' as in regexps.
 [2003-01-21 00:45 UTC] sniper@php.net
This has been documented but it could also mention, that
if the end tag of heredoc is not 'clean', ie. it's not
exactly 'heredoc;' like in the example above, then it is
NOT considered the end tag. 

ZE will ignore it and continued looking for the end tag..
and as it is not found, there will be a parse error, with
line number being the last line of the script.

 [2003-01-21 01:30 UTC] philip@php.net
This behavior has now been documented:
http://cvs.php.net/cvs.php/phpdoc/en/language/types.xml

Thanks for the report :)
 
PHP Copyright © 2001-2025 The PHP Group
All rights reserved.
Last updated: Sun Aug 03 22:00:03 2025 UTC