Solved

What is the correct way to format curly braces in PHP

Posted on 2016-09-13
5
44 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 108

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 108

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

910 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

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now