dreamwest Posted January 4, 2008 Share Posted January 4, 2008 Question - why use {} brackets in an "if else" script, considering it returns the same results?? Without {} Brackets: <?php $age = 4; if ($age > 10) echo 'False 10 is greater than 4'; else if ($age == 4) echo 'This is correct'; else echo 'True 10 is greater than 4'; ?> Returns - "This is correct" With {} Brackets: <?php $age = 4; if ($age > 10){ echo 'False 10 is greater than 4'; }else if ($age == 4){ echo 'This is correct'; }else{ echo 'True 10 is greater than 4'; } ?> Returns - "This is correct" Quote Link to comment https://forums.phpfreaks.com/topic/84444-solved-why-use-in-if-else-scripts/ Share on other sites More sharing options...
rajivgonsalves Posted January 4, 2008 Share Posted January 4, 2008 Well {} are always good programming practice it defines the boundary of the if statement the code without brackets is one line code... what would you do if you had a two line code ?? Quote Link to comment https://forums.phpfreaks.com/topic/84444-solved-why-use-in-if-else-scripts/#findComment-430134 Share on other sites More sharing options...
redbullmarky Posted January 4, 2008 Share Posted January 4, 2008 as rajivgonsalves said, it's good practice. the majority of if/else statements will have more than one line in the blocks, so the brackets are then required. using brackets also for just single line statements keeps things consistent. i'm sure there are other reasons, but those are mine. Quote Link to comment https://forums.phpfreaks.com/topic/84444-solved-why-use-in-if-else-scripts/#findComment-430144 Share on other sites More sharing options...
zq29 Posted January 4, 2008 Share Posted January 4, 2008 The only time I don't use brackets is for a line like this: if($x > 0) $y /= $z; Otherwise, I use the brackets purely for consistency and readability. Unless there's more than one line, then they have to be used anyway! Quote Link to comment https://forums.phpfreaks.com/topic/84444-solved-why-use-in-if-else-scripts/#findComment-430185 Share on other sites More sharing options...
neylitalo Posted January 4, 2008 Share Posted January 4, 2008 I use {} even when there's only one line in the conditional - just in case I need to add another line in the future. I don't want to have to add the {} as well as the additional line(s). Quote Link to comment https://forums.phpfreaks.com/topic/84444-solved-why-use-in-if-else-scripts/#findComment-430200 Share on other sites More sharing options...
roopurt18 Posted January 4, 2008 Share Posted January 4, 2008 It's best practice to always use them and usually makes your life less of a headache in the long run, for lots of reasons. It's always best to make your intentions clear: if( $x > $y ) $x++; $y--; When looking at that snippet 6 months from now, did you intend for both to be in the if statement or just the $x++? Without having the brackets there you may not know what you originally intended. if( $x > $y ) $x++; if($x > $z) $y++; else $z-- Which if does that else statement belong to? The indentation is misleading, but the interpreter will stick it with the inner-if if I'm not mistaken. Again, we run into the problem of original intent when looking at this 6 months later. In terms of headache avoidance, you might think now that you'll not use curly brackets if they're not necessary and add them if they are. Let's say you are debugging this code: if($x > $y) $x++; You want to check the value of $x before the increment by adding an echo statement. This is a statement you intend to remove, so you don't put the curly brackets in: if($x > $y) echo $x; $x++; Well now the echo is the if body and the $x++ will always increment, regardless of the condition. You're already trying to fix one bug and now you've introduced another, so good luck with that. There are many examples that can be given as to why to do it, but the reasons almost always fall back to avoiding being careless and original intent. Given the amount of frustration that is likely to be obtained by not using them versus the time it takes to put them in the code it doesn't make sense from an economical standpoint to not use them. Or in other words, you can lose countless hours by not using them or take less than 1 second to put them there in the first place. Quote Link to comment https://forums.phpfreaks.com/topic/84444-solved-why-use-in-if-else-scripts/#findComment-430247 Share on other sites More sharing options...
dreamwest Posted January 4, 2008 Author Share Posted January 4, 2008 I think ive found a better , less bulky way for this. Original code: <?php $age = 4; if ($age > 10){ echo 'False 10 is greater than 4'; }else if ($age == 4){ echo 'This is correct'; }else{ echo 'True 10 is greater than 4'; } ?> Alternate method (2 lines code, instead of eight): <?php $age = 11; echo ($age == 11)? 'Number exactly 11' : 'Not 11'; ?> Quote Link to comment https://forums.phpfreaks.com/topic/84444-solved-why-use-in-if-else-scripts/#findComment-430289 Share on other sites More sharing options...
rajivgonsalves Posted January 4, 2008 Share Posted January 4, 2008 where you asking a question? ... or expecting an answer ? Quote Link to comment https://forums.phpfreaks.com/topic/84444-solved-why-use-in-if-else-scripts/#findComment-430295 Share on other sites More sharing options...
Liquid Fire Posted January 4, 2008 Share Posted January 4, 2008 I think ive found a better , less bulky way for this. Original code: <?php $age = 4; if ($age > 10){ echo 'False 10 is greater than 4'; }else if ($age == 4){ echo 'This is correct'; }else{ echo 'True 10 is greater than 4'; } ?> Alternate method (2 lines code, instead of eight): <?php $age = 11; echo ($age == 11)? 'Number exactly 11' : 'Not 11'; ?> first of all these 2 sets of code do not do the same thing, the ternary operator is to replace an if/else statement not a if/else if/else(I don't use ternary operator that much so someone correct em if I am wrong). second i would code the first example like <?php $age = 4; if ($age > 10) { echo 'False 10 is greater than 4'; } else if ($age == 4) { echo 'This is correct'; } else { echo 'True 10 is greater than 4'; } ?> because personal it make me see where a block end a bit faster even tho this case would not matter, I have seen some code where I could not easily find the end of a particular bolck as fast as I could if it were written like this. It all comes down to personal preference. Quote Link to comment https://forums.phpfreaks.com/topic/84444-solved-why-use-in-if-else-scripts/#findComment-430324 Share on other sites More sharing options...
redbullmarky Posted January 4, 2008 Share Posted January 4, 2008 you can use the ternary operator for multiple if/else, but i'd recommend against it due to readability. i use it, but only for very simple stuff. example of the above code with ternary: <?php $age = 4; echo ($age > 10) ? 'False 10 is greater than 4' : (($age == 4) ? 'This is correct' : 'True 10 is greater than 4'); ?> not exactly readable.... Quote Link to comment https://forums.phpfreaks.com/topic/84444-solved-why-use-in-if-else-scripts/#findComment-430328 Share on other sites More sharing options...
roopurt18 Posted January 4, 2008 Share Posted January 4, 2008 I think ive found a better , less bulky way for this. I really wouldn't worry about bulk. Once your script starts getting into the hundreds of lines of code, who cares about a few extra lines here and there. There are two golden rules you should follow, IMO, when writing any and all of your code: 1) Do not write any lines greater than 80 characters in length 2) If a function starts getting longer than 25-30 lines, consider breaking it down into more functions internally. In regards to the ternary operator, I use it strictly as a replacement for what would be the most simple of if-else statements, usually when setting a variable to one of two values based on a simple condition. However, you could still accomplish a lot of functionality with the ternary operator and still keep your code readable, like so: $var = $var_a == $var_b ? one_function() : another_function(); Quote Link to comment https://forums.phpfreaks.com/topic/84444-solved-why-use-in-if-else-scripts/#findComment-430523 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.