|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
[2009-06-20 10:06 UTC] simon at stienen dot name
|
|||||||||||||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Thu Oct 30 16:00:01 2025 UTC |
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.