Using if ... else ... or simply if ... to determine what is returned

What's better?

function test($val = 'a') { if($val == 'a') { return true; } return false; } 

or

 function test($val = 'a') { if($val == 'a') { return true; } else { return false; } } 

In fact, they do the same. If $val not 'a', the function returns false. Just personal preferences?

+7
php if-statement
source share
9 answers

I think it comes down to how the comparison "feels" you. I would use the first, if it seemed that $ val is "a", this is a special case, and usually the function returns false. I would use the second if it was like it was 50/50, however that may be.

+7
source share

They are the same. However, for this case, I prefer:

 function test($val = 'a') { return ($val == 'a'); } 
+22
source share

Compared to this, I prefer the latter for clarity. However i prefer

 return ($val == 'a'); 
+9
source share

In PHP, if nothing is done in the function and the end is reached, it will be as if it returned false. Because of this, you never need to return false unless there is nothing else in the function. This leaves us with this:

 function test($val = 'a') { if($val == 'a') { return true; } } 

If the if, elseif or else command has only one command, the brackets ("{" "}") are not needed, which forces us to end up:

 function test($val = 'a') { if($val == 'a') return true; } 

In PHP, you can actually return a comparison that will be executed immediately before returning it. Here is what some of the people who replied to this post suggested. Doing this leaves us with this code:

 function test($val = 'a') { return ($val == 'a'); } 

True will be returned if the "($ val == 'a')" block is true, otherwise false will be returned, since this is not true. Logics.

I actually tend to use the second convention that I presented, out of habit. Having seen the beauty of the simplicity of the third presented by others, I will probably switch to this when applicable.

EDIT:

If you want to write code that is easier to understand for non-PHP specialists, another alternative would be:

 function test($val = 'a') { if($val == 'a') return true; else return false; } 

I would say that not using curly braces in the circumstances described in my second example gives you a more readable code, since curly braces tend to make your code look messy if they don't contain multiple lines.

+3
source share

I really believe that I have ever tried to pass code. If you look at this:

 function test($val = 'a') { if($val !== 'a') { return false; } return true; } 

You can see that he is doing the same thing as your examples, but he has a different intention. For this example, it really does not make sense to have:

 function test($val = 'a') { if($val !== 'a') { return false; } else { return true; } } 

I think this is @Ned because it is trying to pass the intent for the if operation.

+2
source share

If you did not return boolean , I would choose the first form. In this case, I would simply do:

 return ($val == 'a'); 

as others suggested.

+1
source share

I would stick with the first, the simpler the code (and easy to read), the better.

0
source share

While you are reading the second block of code. You can easily understand that it returns false when val is not equal to "a".

But in the first block of code, it's a little hard to understand when it returns false. It is not so difficult in this example, but I assume that your if statements will not be so simple.

0
source share

Instead

 function test($val = 'a') { if($val == 'a') { return true; } else { return false; } } 

The below approach is better, there is no more else overhead, and the code is also short:

 function test($val = 'a') { if($val == 'a') { return true; } return false; } 

This can be made even shorter:

 function test($val = 'a') { return ($val == 'a'); } 
0
source share

All Articles