7

I'm debugging a client's 3500-line PHP file. Including this file causes a PHP Parse error: syntax error, unexpected $end in ... error, so I'm assuming there's a missing brace somewhere. Is there a simple tool or method for missing brace discovery, either online or in the Komodo IDE I'm using?

Yarin
  • 173,523
  • 149
  • 402
  • 512
  • 4
    3000-line file? Yuk. http://stackoverflow.com/questions/378959/is-there-a-static-code-analyzer-like-lint-for-php-files – Frankie Feb 13 '12 at 18:43
  • In most IDEs, you can find the missing braces by looking for the place where every single line becomes an error. – kitti Feb 13 '12 at 18:44
  • NetBeans :) thats what i have in mind. . . – tomexsans Feb 13 '12 at 18:45
  • Using vi you could simply use % to bounce on brackets and see where things don't line up anymore. – Marc B Feb 13 '12 at 18:51
  • 1
    Make a copy of the file and comment out / remove parts of the code until you don't get any complaints. This makes it easier to debug Just make sure that if you actually do something (like modifying a database/file) you remove that code as well so you don't mess anything out. – Optimist Feb 13 '12 at 18:58
  • Did you say 3,5k lines in a single file? – Pavel May 01 '12 at 06:24
  • @Yarin, I usually have PHP files with a few thousand lines. Everyone else, please stop complaining. If OP likes not having lots of (redundantly partitioned) different files, or can and prefers to manage a single file with that much (probably neater and more-commented) code then that's his call. Optimization? Negligible, and only if you `include` within given conditions. I am totally open to splitting my files up, however nothing has convinced me to do so thus far. /rant_over – Eugene Mar 16 '17 at 05:46
  • Oh, and I second @Optimist's response, I usually cull functions until it works again (i.e. different error due to culled functions missing!), typically divide and conquer. – Eugene Mar 16 '17 at 05:48

7 Answers7

2

another option (similar to notepad++) is to use Dreamweaver to find the associated closing tag.
See this link: How do I make Dreamweaver to show me closing tags?

in dreamweaver:
To select matching opening and closing curly braces, brackets, or parentheses, click inside the opening or closing symbol, and click the Balance Braces button on the Coding toolbar (it's immediately below Select Parent Tag). Alternatively, use the keyboard shortcut, Ctrl+'/Cmd+'.

Community
  • 1
  • 1
2

Use consistent and clean tabbing. I've found that makes it very hard to miss a closing brace.

Other than that, you've kinda dug your own grave here. What are you coding that results in a 3500-line PHP file?

EDIT: Try dumping your code into Notepad++. I'm fairly sure that will highlight the associated closing brace if you click on the opening one, but with larger files, I've gotten somewhat unreliable performance with this.

tnw
  • 13,521
  • 15
  • 70
  • 111
  • Tory- Nice advice if you birth your own code, but this is client code, not mine- I'm just parachuting in as a consultant. – Yarin Feb 13 '12 at 19:09
  • 1
    This does not take into account the alternate format `if(true): else: endif;` – Ray Jun 28 '12 at 15:38
1

Komodo Edit has a nice feature that highlights what's inside braces

Ctrl+Alt+]

vlad
  • 11
  • 1
1

I'm just working on a related problem (find missing array square bracket in JSON-object). So I hope to be able to help.

     $pos=0;
     $braceCount=0;
while( preg_match('~\{|\}~S', $source, $out, PREG_OFFSET_CAPTURE, $pos) ){
     if($out[0][0] === '{'){
       $braceCount++;
       if( $braceCount === 1 )$startPos=$out[0][1];
     }
     elseif( $out[0][0] === '}' ){
       $braceCount--;
       if( $braceCount === 0 ){
         //echo 'Up to that position:'.$out[0][1].' every thing seems to be ok?<br>';
         echo substr($source,$startPos,($out[0][1]+1-$startPos)).'<br>';
       } 
       elseif( $braceCount < 0 ){
         echo 'To many closing brace right before '.($out[0][1]+1).'<br>';
         exit;
       }
     }
     $pos = $out[0][1]+1;
}
if( $braceCount > 0 ) echo 'Closing brace is missing somewhere.'; 

This echo the source until there is a miss match and an error for curly braces.

B.F.
  • 477
  • 6
  • 9
1

Use NetBeans IDE for PHP.
http://netbeans.org/features/php/

Will check your syntax and highlight issues amongst the many other nice features it has. And it's free.

Jeff
  • 370
  • 1
  • 2
  • 11
0

We really appreciate how smart everybody is! Sometimes its better to stick to the tool the person is using and exactly what they are asking about. I don't know if you have found your answer but here is what I use.

  1. Put your cursor on the opening Brace then -
  2. On Mac: [Command]'

or Edit Menu > Balance Braces 3. Result: This will highlight the code on Dreamweaver between the braces and help resolve the coding issue.

Hope this helps. P.S. Sometimes you need 3000 lines of code :)

0

What text editor are you using? I will recommend you to use Eclipse and it will be much easier to detect it :) or also you can public your code and may we can help in that way :)

locrizak
  • 12,192
  • 12
  • 60
  • 80
Sabri Aziri
  • 4,084
  • 5
  • 30
  • 45
  • I'm using Komodo IDE and I can't make the code public as it's proprietary client code. – Yarin Feb 13 '12 at 19:06
  • I'm not sure how does Komodo IDE work but I highly will recommend you for php,javascript,html... to start working with Eclipse [HERE](http://www.eclipse.org/downloads/) or you can send to me your code by email =] sa18890@seeu.edu.mk This is all i can do :) – Sabri Aziri Feb 14 '12 at 14:27