php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Doc Bug #76608 Heredoc end label should not require an additional trailing character
Submitted: 2018-07-11 01:01 UTC Modified: 2023-01-02 02:03 UTC
Votes:6
Avg. Score:3.3 ± 1.4
Reproduced:4 of 5 (80.0%)
Same Version:2 (50.0%)
Same OS:2 (50.0%)
From: mattacosta at gmail dot com Assigned:
Status: Verified Package: Scripting Engine problem
PHP Version: 7.2.7 OS:
Private report: No CVE-ID: None
Welcome back! If you're the original bug submitter, here's where you can edit the bug or add additional notes.
If you forgot your password, you can retrieve your password here.
Password:
Status:
Package:
Bug Type:
Summary:
From: mattacosta at gmail dot com
New email:
PHP Version: OS:

 

 [2018-07-11 01:01 UTC] mattacosta at gmail dot com
Description:
------------
If a perfectly valid heredoc is not followed by an additional character, the reported error states that an end label was expected, even though it was present. 

This means that instead of removing the requirement for a trailing newline, it was in fact replaced by a requirement for any trailing character.

Test script:
---------------
// No trailing line.
echo <<<LABEL
  hello world
  LABEL

Expected result:
----------------
Parse error: syntax error, unexpected end of file, expecting ',' or ';'

Actual result:
--------------
Parse error: syntax error, unexpected end of file, expecting variable (T_VARIABLE) or heredoc end (T_END_HEREDOC) or ${ (T_DOLLAR_OPEN_CURLY_BRACES) or {$ (T_CURLY_OPEN)

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2018-07-11 14:55 UTC] cmb@php.net
-Status: Open +Status: Verified -PHP Version: 7.3.0alpha3 +PHP Version: 7.2.7
 [2018-07-11 14:55 UTC] cmb@php.net
This does not appear particularly related to PHP 7.3, see
<https://3v4l.org/CeumU>.
 [2018-07-11 17:57 UTC] mattacosta at gmail dot com
That error appears to be correct in previous versions though. Without the required  line break, the label would be part of the text instead, so it would make sense to expect T_END_HEREDOC.
 [2019-04-03 17:47 UTC] chasepeeler at gmail dot com
Yes, in 7.3, the documentation states:
"The closing marker for doc strings is no longer required to be followed by a semicolon or newline."
https://www.php.net/manual/en/migration73.new-features.php#migration73.new-features.core.heredoc

However, if you don't include a semicolon, you get an error:

<?php

$str = <<<FOO
abcdefg
FOO

echo $str;

> Parse error: syntax error, unexpected 'echo' (T_ECHO) in /in/eTKHe on line 7

If you add a semicolon after FOO, everything works. This is against 7.3.3:
https://3v4l.org/eTKHe
 [2019-04-03 18:07 UTC] requinix@php.net
> However, if you don't include a semicolon, you get an error:
The semicolon is not required *for the heredoc itself*. You still have to write valid code, and

  $str = "
  abcdefg"

  echo $str;

is not valid code.
 [2019-04-03 19:09 UTC] chasepeeler at gmail dot com
Can you provide an example where you would ever not need a semicolon at the end of the heredoc, then?
 [2019-04-03 19:18 UTC] requinix@php.net
Yeah. Easily.

echo <<<FOO
abcdefg
FOO, "hijk";
 [2019-09-06 21:47 UTC] jasny@php.net
-Assigned To: +Assigned To: jasny
 [2021-10-07 10:12 UTC] cmb@php.net
-Type: Bug +Type: Documentation Problem
 [2021-10-07 10:12 UTC] cmb@php.net
> The semicolon is not required *for the heredoc itself*. You
> still have to write valid code, […]

Right.  Changing to doc problem.
 [2023-01-02 02:03 UTC] jasny@php.net
-Assigned To: jasny +Assigned To:
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Sat Dec 21 16:01:28 2024 UTC