Solved

To use { or not to use { - that is the question

Posted on 2016-09-09
3
40 Views
Last Modified: 2016-09-09
Since I started learning php recently, I always use { after IF. For example:

if ($_SERVER['REQUEST_METHOD'] == "POST") {
            echo "okay";
      }

I was watching a tutorial the other day and noticed that the person did not use those braces e.g.:

if ($_SERVER['REQUEST_METHOD'] == "POST")
            echo "okay";
      
I tried this myself and the code still worked. I am confused now as to whether I need the braces or not if I don't have an else to add afterwards. I would rather use the braces always so I don't forget about them but just wondered if it is okay to do so even if there isn't an ELSE accompanying the IF.
0
Comment
Question by:Black Sulfur
3 Comments
 
LVL 31

Accepted Solution

by:
Marco Gasi earned 500 total points
ID: 41791814
If the command i only one you could avoid them, but if you have many lines of code to execute if the condition is true then parenthesis are mandatory. Personally I always use them, even if the line code to run is only one as in your example because they make more readable the code and help me to understand without any doubt which code runs if a condition is true and which code runs anyway.
1
 
LVL 108

Expert Comment

by:Ray Paseur
ID: 41791861
What Marco said.  He earned the points.

These things are usually dictated by a "coding standard" that is probably prescribed by your clients or employer.  Coding standards have important effects on things like automated documentation tools.

I always do what the paying customer says!
1
 
LVL 34

Expert Comment

by:gr8gonzo
ID: 41791912
My recommendation is ALWAYS use braces! ALWAYS!

1. Always assume someone else will come along and have to work on code that you wrote. Let's say someone else comes along and is debugging your code and does this:

if ($_SERVER['REQUEST_METHOD'] == "POST")
           file_put_contents("debug.log","Request method was POST\n");
            echo "okay";

Their additional line of code now breaks the logic and now "okay" will be echo-ed, no matter what the REQUEST_METHOD is. However, the indenting makes it sort of look like the "echo" line is still conditional, even though it's not anymore. So without braces, it makes it much easier for code to be broken accidentally.

2. Many code editors rely on braces to allow you to expand/collapse code. They'll even highlight matching braces so you can easily tell where a block of code begins and ends. Without braces, most editors can't tell that it's a conditional line. Some editors might even think it's bad code or not realize where the block ends properly!

3. Many code parsers / beautifiers rely on braces.

4. Developers who know other languages but are new to PHP may not realize that it's a conditional statement without braces, so it helps other people to read PHP code.

5. There's really no advantage to NOT using braces except to spare a couple of keystrokes here and there.

Sometimes if I'm doing "quick conditional lines", I'll do it all on one line but I'll still use braces, which can make those kinds of lines much easier to visible parse/read and move around:

if($debugMode) { echo __LINE__ . " :: Hello world!\n"; }
2

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

Suggested Solutions

Title # Comments Views Activity
object oriented programming comparison 5 54
What is the best PDF generator to use? 1 28
Update from TABLE-A to TABLE-B 5 39
How to convert my query to the proper format? 5 17
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 …
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…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
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 …

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