While coding, i just asked myself this question :
Is this faster :
if(false) return true;
else return false;
Than this ?
if(false) return true;
return false;
Of course, IF there is a difference it is ridiculous, but my curiosity won't leave until i know that :D
return is fine and easy to read if you check some boolean condition (maybe setting) that would make the whole following code unnecessary to be executed at all. if .. else is much easier to read if you expect some value to be either of two (or more) possible values and you want to execute different code for both cases.
Yes, the order of the conditions matters.
The return statement returns the flow of the execution to the function from where it is called. This statement does not mandatorily need any conditional statements. As soon as the statement is executed, the flow of the program stops immediately and returns the control from where it was called.
The if/else if statement allows you to create a chain of if statements. The if statements are evaluated in order until one of the if expressions is true or the end of the if/else if chain is reached. If the end of the if/else if chain is reached without a true expression, no code blocks are executed.
Just:
return !false;
So in real-life example
return !$this->isSth();
// Not
if ($this->isSth) {
return false;
} else {
return true;
}
Performance isn't important here - every solution is extremely fast, there is no need for optimization. Remember the words of Donald Knuth:
Premature optimization is the root of all evil
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With