Solved

What is the correct way to format curly braces in PHP

Posted on 2016-09-13
5
37 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
Comment Utility
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 82

Assisted Solution

by:Dave Baldwin
Dave Baldwin earned 125 total points
Comment Utility
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
Comment Utility
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 30

Accepted Solution

by:
Marco Gasi earned 250 total points
Comment Utility
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
Comment Utility
Thanks
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
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…
The viewer will learn how to dynamically set the form action using jQuery.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

762 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

12 Experts available now in Live!

Get 1:1 Help Now