Jump to content

[SOLVED] Why use {} in "if / else" scripts


dreamwest

Recommended Posts

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"

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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!

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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';
?>

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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....

Link to comment
Share on other sites

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();

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.