Solved

What is the correct way to format curly braces in PHP

Posted on 2016-09-13
5
57 Views
Last Modified: 2016-09-15
Which one of these is correct?

Screen-Shot-2016-09-13-at-11.16.55-P.pngScreen-Shot-2016-09-13-at-11.17.06-P.png
0
Comment
Question by:burnedfaceless
5 Comments
 
LVL 110

Assisted Solution

by:Ray Paseur
Ray Paseur earned 125 total points
ID: 41797258
Doesn't really matter, so long as the output is correct.  Coding standards are generally applied by the organization that you work for, so just use whatever they ask of you.

As a general rule, I think it's wise to make sure your code is easy to read and your intent is clear.  For me, the number one thing about control structures (curly braces) is to line them up vertically so anyone else reading the code can readily discern the cyclomatic complexity at a glance.

Maybe make a Google search for "cyclomatic complexity" and consider ways to reduce it in your programming.  This has been one of the most valuable lessons I've learned in the last few years.
2
 
LVL 83

Assisted Solution

by:Dave Baldwin
Dave Baldwin earned 125 total points
ID: 41797308
I use the first method except that I indent the closing brace.  What I don't do is go back and change everything... unless the code isn't working.  Then it may get a lot of re-formatting until it works.  A lot of the code I work on was originally written by someone else.
0
 
LVL 110

Assisted Solution

by:Ray Paseur
Ray Paseur earned 125 total points
ID: 41797650
Afterthought... Learn about "linter," "sniffer," and similar code checker scripts that check your code and provide hints for improvement.  PHPStorm (the best IDE by far) has this built-in now.
0
 
LVL 31

Accepted Solution

by:
Marco Gasi earned 250 total points
ID: 41798129
There is no a 'correct' way but several different coding standards. It's up to you choose one and the more important thing is: once you have choose a coding standard, respect it.

But coding standard is about readability of your code: you should even create a new one personal coding standard following your personal taste, nad your code would still work fine if it doesn't contain syntax errors.

You can fine here a list of most known coding standards: http://www.phptherightway.com/ Look at the Chapter 3 "Code Style Guide".

There you find even links to popular tools like PHP_CodeSniffer which can even adjust automatically your code to follow a specified coding standard.
0
 

Author Closing Comment

by:burnedfaceless
ID: 41800099
Thanks
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

735 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question