php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #47883 wish: syntax error, unexpected $end should show start point
Submitted: 2009-04-02 22:01 UTC Modified: -
Votes:5
Avg. Score:4.0 ± 0.9
Reproduced:5 of 5 (100.0%)
Same Version:1 (20.0%)
Same OS:1 (20.0%)
From: php at richardneill dot org Assigned:
Status: Open Package: Feature/Change Request
PHP Version: 5.2.9 OS: Linux
Private report: No CVE-ID: None
 [2009-04-02 22:01 UTC] php at richardneill dot org
Description:
------------
If I have a script with a syntax error, such as a missing '}' somewhere, PHP will helpfully tell me that there's a problem, but direct me to look in the wrong place.

The error message is as follows:
   Parse error: syntax error, unexpected $end in [FILENAME] at 
   [NUMBER_OF_LAST_LINE]

This is unhelpful for 2 reasons.

 * It isn't clear what type of syntax error would cause this - there's no way to know what character PHP was expecting to see. 

 * I have no easy way of finding the offending line where the bug really lies, apart from commenting out parts of the source and doing a binary-search within it. Essentially, PHP is saying "You have an error, but I won't tell you where".



Expected result:
----------------
The following would be clearer and *far* more helpful.

  Syntax error: unexpected $end in [FILENAME] at LINENUM: missing
  an expected '}' character. (The corresponding, unmatched '{' is
  probably at line xxx.)



Of course, it's not always possible for PHP to know which open-brace is the unmatched one - however, it could usually take a reasonable best guess by iterating *backwards* through the source, and doing:
 
 1  $nest_depth=0
 2  If a '}' is encountered, $nest_depth++
 3  If a '{' is encountered, $nest_depth-- 
 4  If $nest_depth == -1, we have located the open-brace that isn't  
    closed. Print LINENUM.





Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2009-06-20 10:06 UTC] simon at stienen dot name
I agree, having an actual line number would be nice. I'd consider it more important to know what kind of token caused the unexpected $end.

Interestingly, it tells me if it's missing a ) at the end, but for " or }, it's just "unexpected $end".
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Thu Nov 21 14:01:29 2024 UTC