Solved

What is the correct way to format curly braces in PHP

Posted on 2016-09-13
5
72 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

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…
Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to count occurrences of each item in an array.

630 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