Solved

What is the correct way to format curly braces in PHP

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

Don't Cry: How Liquid Web is Ensuring Security

WannaCry is just the start. Read how Liquid Web is protecting itself and its customers against new threats.

Question has a verified solution.

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

Popularity Can Be Measured Sometimes we deal with questions of popularity, and we need a way to collect opinions from our clients.  This article shows a simple teaching example of how we might elect a favorite color by letting our clients vote for …
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
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.

729 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