Link to home
Start Free TrialLog in
Avatar of bleggee
bleggeeFlag for United States of America

asked on

PHP Nesting question

Hi - Given a PHP website file, what is the easiest way to see the nesting of the {  and   } brackets?
(Indented, Colorized, etc)

I have latest versions of Dreamweaver, Coda 2, BBEDIT, and PHPStorm, of course any other 3rd party utility will do!
Avatar of Chris Stanyon
Chris Stanyon
Flag of United Kingdom of Great Britain and Northern Ireland image

Depends what you mean by 'easiest'. Any decent IDE should show matching control blocks (click on an opening bracket, and it will show you where the ending bracket is). If you don't have good indentations than many offer a 'code tidy' option to sort that out for you - may sound trivial, but good indentation in code makes your life a lot easier. Some IDEs also allow you to collapse control blocks with a little arrow in the margin, so you can hide unwanted blocks. Take a look at VSCode as an IDE - free and pretty decent at handling PHP

And sometimes, using the alternative control blocks in PHP can help quite a bit - particularly if you have lots of nested curly brackets:

while (...) :

    if (...) :

        for (...) :
            
        endfor;

    endif;

endwhile; 

Open in new window

Sometime this makes your code easier to read :)
Firstly - code so you don't end up with too much nesting. If you are finding you are going 3/4/5 levels deep you are probably needing to refactor.

Secondly - neat code - make sure your indenting is exact on all your code - messy code = bad code.

Third - as Chris said any decent editor will do the matching for you - when you position your cursor on one it highlights the closing bracket - they should also support collapsible blocks so you can collapse bracketed blocks you are not interested in.

An addendum to the second point - there are no points for writing the most code on one line.. Break your code up so it is easily readable - that means within the limit (horizontal) of your editor pane. If necessary break your line up or refactor into several lines.

The goal with your code is that you can read it - easily - by looking it it without having to scroll the window or pull apart complex code on a single line.
Avatar of bleggee

ASKER

Thanks guys.  I appreciate all the "Write Clean Code" comments, though the files I am working with were written elsewhere ... I inherited them. Very Spagetti coded!!
If I need to go the refactoring route, have either of you used any of the automatic refactoring tools that come with XCode or VS?
I've not used the automatic tools, but I can't imagine they'll give you particularly good results. Some of the code may be improved by using the tools, but large parts of it will likely come down to your own personal choice and programming styles. You can run it through a Code Sniffer to see if any problems are highlighted, and that may go some way to improving it.

In my experience, generally speaking, bad code can't be made into good code by simply running a tool.
I have used VS Code refactoring but not with PHP - mostly with Typescript - it works well - not familiar with the other environments.
ASKER CERTIFIED SOLUTION
Avatar of lenamtl
lenamtl
Flag of Canada image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial