devWhiz Posted January 25, 2012 Share Posted January 25, 2012 I've noticed some people don't use { } when they right statements, when I first started learning PHP a couple years ago, I thought you HAD to put the curly brackets, but apparently not, how does this work and why don't some people use them in their scripts? Quote Link to comment https://forums.phpfreaks.com/topic/255788-reason-for-not-using-brackets-on-conditional-statements-function-etc/ Share on other sites More sharing options...
Psycho Posted January 25, 2012 Share Posted January 25, 2012 The basic syntax of the control structures (if, while, etc.) is just a single line of code after the control structure with no braces. You only need the braces if you have multiple lines to be associated with the control structure. (see this reference: http://us3.php.net/manual/en/control-structures.while.php) I don't think the same applies for functions. But, I ALWAYS use the braces with indenting of code to make my scripts easy to read/analyze There is also a third option: http://us3.php.net/manual/en/control-structures.alternative-syntax.php Quote Link to comment https://forums.phpfreaks.com/topic/255788-reason-for-not-using-brackets-on-conditional-statements-function-etc/#findComment-1311219 Share on other sites More sharing options...
wigpip Posted January 25, 2012 Share Posted January 25, 2012 if there is a very short conditional that fits on one line and only has one outcome then it's ok but most agree that it's not such a good idea as when you leave out the {} from statements it can lead to problems later in the life of the code, for example you might want to add an elseif to the statement or grow the argument to multiple lines. Personally it's just easier to read code with {} added regardless. Quote Link to comment https://forums.phpfreaks.com/topic/255788-reason-for-not-using-brackets-on-conditional-statements-function-etc/#findComment-1311223 Share on other sites More sharing options...
scootstah Posted January 25, 2012 Share Posted January 25, 2012 Some people find it more convenient to write if ($i == 1) doSomething(); than if ($i == 1) { doSomething(); } Both ways do the exact same thing so it's just a preference thing. You can also use the syntax Psycho posted which is usually favorable for templates when you are going in and out of PHP tags. For example, <?php if ($i == 1): ?> some html here <?php endif; ?> is a lot cleaner and more readable than <?php if ($i == 1) { ?> some html here <?php } ?> You can also use this syntax with foreach and while loops which is again more readable. if there is a very short conditional that fits on one line and only has one outcome then it's ok but most agree that it's not such a good idea as when you leave out the {} from statements it can lead to problems later in the life of the code, for example you might want to add an elseif to the statement or grow the argument to multiple lines. Personally it's just easier to read code with {} added regardless. You can use else and elseif while omitting brackets. if ($i == 1) doSomething(); else if($i == 2) doSomethingElse(); else dontDoAnything(); This works just fine. Quote Link to comment https://forums.phpfreaks.com/topic/255788-reason-for-not-using-brackets-on-conditional-statements-function-etc/#findComment-1311224 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.