maffers
maffers

Reputation: 9

What is the difference between (if (x == y) return true, else false) and (if (x != y) return false, else true)?

if($send != true)
    {
        echo "Error Sending Email: ". $send->message;
        return false;
    }
    else 
    {
        echo "Mail Sent";
        return true;
    }

I'm learning PHP right now, and am struggling to understand why this code is set up the way it is. Is there any difference between the above code and this:

if($send == true)
    {
        echo "Mail Sent";
        return true;
    }
    else 
    {
        echo "Error Sending Email: ". $send->message;
        return false;
    }

It seems to me that they would do the same thing, and I can't think of any reasons why to use one over the other. Is it just preference, or am I missing something? I'm a beginner so any input would be greatly appreciated.

Upvotes: 1

Views: 1469

Answers (2)

David Wyly
David Wyly

Reputation: 1701

I prefer to do my error validation first, so that I don't have to wrap everything following my error validation into its own separate conditional.

Consider the following:

if(!$send) {
    echo "Error Sending Email: ". $send->message;
    return false;
}
echo "Mail Sent";
return true;

For me this is just easier to read, and becomes much easier to manage when dealing with multiple validation criteria. Just throw all your validation criteria in at the beginning and you're gold (assuming that, when you have an error, you exit the function). Once it passes every validation check, it just chugs right along.

The main take away, however, is that there is no "right" answer. It's strictly a matter of preference.

Upvotes: 5

Victor Smirnov
Victor Smirnov

Reputation: 3780

There is no difference and mostly you can do whatever you like.

Please keep in mind that you do not need to compare boolean values with true and false cause they are already okay for the condition. For example

if ($send) {
    echo "Mail Sent";
    return true;
} else {
    echo "Error Sending Email: ". $send->message;
    return false;
}

You can also return the $send value like this

if ($send) {
    echo "Mail Sent";
} else {
    echo "Error Sending Email: ". $send->message;
}
return $send;

When you choose what order to use I suggest to think about which one is easier to read and understand.

p.s. One of the important topics in PHP is Type Juggling PHP automatically converts different types to whatever is needed. Just read this and keep in mind when you do different conditional checks. Sometimes this might be tricky.

p.p.s. There is a great book my Martin Fowler - Refactoring: Improving the Design of Existing Code It covers questions like this - how to organize the code, what is the best way to do conditional checks etc. Well, this is not the only book about the subject but this was the first one I thought about. It might be arguable and not all people follow his recommendation but this is something you might want to know :)

Upvotes: 6

Related Questions