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 undefined
Last Comment
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 (...) :



Open in new window

Sometime this makes your code easier to read :)
Avatar of Julian Hansen
Julian Hansen
Flag of South Africa image

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
Flag of United States of America image


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?
Avatar of Chris Stanyon
Chris Stanyon
Flag of United Kingdom of Great Britain and Northern Ireland image

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.
Avatar of Julian Hansen
Julian Hansen
Flag of South Africa image

I have used VS Code refactoring but not with PHP - mostly with Typescript - it works well - not familiar with the other environments.
Avatar of lenamtl
Flag of Canada image

Blurred text
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial

PHP is a widely-used server-side scripting language especially suited for web development, powering tens of millions of sites from Facebook to personal WordPress blogs. PHP is often paired with the MySQL relational database, but includes support for most other mainstream databases. By utilizing different Server APIs, PHP can work on many different web servers as a server-side scripting language.

Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews


IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo