What is wrong with this if
statement?
if($condition){ return true; }else{ return false; }
It will work, but it is unnecessarily verbose. This way is better:
return ($condition);
See the difference? We just turned five lines of code into one and made it a bit more readable.
Don’t think I’m against this structure because there are cases where the more verbose way is better. For example, if you only want to return true, but you want to do something else when the condition is false:
if($condition){ return true; }else{ //do something else }
You could also use the short form if
statement for simple cases like this:
return (($condition)?true:'Condition is false');
Or shorten it a bit more:
return (($condition)?:'Condition is false');
There are several options when building if
statements that return boolean values. Weigh your options considering readability and choose the one that works best for every situation.