Crazy Horse
asked on
To use { or not to use { - that is the question
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.
if ($_SERVER['REQUEST_METHOD'
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'
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.
ASKER CERTIFIED SOLUTION
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
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.l og","Reque st 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"; }
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'
file_put_contents("debug.l
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"; }
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!