Find missing braces in large PHP file?

10,497

Solution 1

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.

Solution 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+'.

Solution 3

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

Ctrl+Alt+]

Solution 4

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.

Solution 5

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.

Share:
10,497
Yarin
Author by

Yarin

Products PDF Buddy - Popular online PDF editor Gems Snappconfig - Smarter Rails app configuration

Updated on July 31, 2022

Comments

  • Yarin
    Yarin almost 2 years

    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
    Yarin over 12 years
    I'm using Komodo IDE and I can't make the code public as it's proprietary client code.
  • Yarin
    Yarin over 12 years
    Tory- Nice advice if you birth your own code, but this is client code, not mine- I'm just parachuting in as a consultant.
  • Sabri Aziri
    Sabri Aziri over 12 years
    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 or you can send to me your code by email =] [email protected] This is all i can do :)
  • Ray
    Ray almost 12 years
    This does not take into account the alternate format if(true): else: endif;
  • ashleedawg
    ashleedawg about 5 years
    would you care to explain what this is, and how to use it?