HH.
HH.

Reputation: 197

Comparison operator return 0 ==

I am reading a book and the author is using the following function. I don't understand the benefit of the equal operator. Can anybody please explain the reason for using the equal operator.

public function isDiscounted()
{
return 0 == $this->getRow()->discountPercent ? false : true;
}

Would it not be easier to go for

public function isDiscounted()
{
return $this->getRow()->discountPercent ? false : true;
}

?

Best regards, Herbert

Upvotes: 1

Views: 1099

Answers (4)

littleibex
littleibex

Reputation: 1712

No, it's not easier to go for

return $this->getRow()->discountPercent ? false : true

In fact, it's wrong. You are supposed to return false if discountPercent is zero. In your suggested code, if discountPercent is zero, it will evaluate to false since it's used as a condition and return true.

If you truly want to keep the same logic but make it shorter the best way to go about it would be:

return $this->getRow()->discountPercent != 0

I have a rule I always follow: If the result of a conditional statement is a boolean, then return/condition itself. Also, I disagree that writing:

return 0 == $this->getRow()->discountPercent ? false : true;

makes the code more readable. If we go by what the function is supposed to do, I would write it something like this:

return $this->getRow()->discountPercent > 0

which clearly indicates that if discountPercent has a value greater than zero then it is discounted. Simple, precise and clear. It also takes care of negative values then.

Upvotes: 0

Barmar
Barmar

Reputation: 780724

The benefit of the == operator is to make the program's intent clearer, that you're comparing a numeric variable with zero. It's equivalent to writing:

if (0 == $this->getRow()->discountPercent) {
    return false;
} else {
    return true;
}

You could also write it as:

return $this->getRow()->discountPercent ? true : false;

but this suggests that discountPercent is a boolean value, not numeric. Similarly, you could write:

return !$this->getRow()->discountPercent;

but this also suggests that it's boolean. While PHP is flexible with types like this, treating all non-falsy values as true, the original code is easier for human readers to understand.

Upvotes: 2

AbraCadaver
AbraCadaver

Reputation: 78994

In your example you would need to swap the true and false:

return $this->getRow()->discountPercent ? true : false;

However you could just cast the integer return to a boolean:

return (bool)$this->getRow()->discountPercent;

Or even:

return 0 != $this->getRow()->discountPercent;

There's no need for the ternary returning true or false.

Upvotes: 4

Jonathan
Jonathan

Reputation: 2877

In PHP you can do a loose comparison or a strict comparison. It really depends what you are trying to compare for, sometimes the loose is fine, sometimes you need it to be strict.

loose vs strict

/** loose **/
0 == 0; // true
0 == false; // true
0 == ''; // true
0 == null; // true

/** strict **/
0 === 0; // true
0 === false; // false
0 === ''; // false
0 === null; // false

as it pertains to your example

/**
 * When doing the loose comparison, anything is isn't
 * 0, false, '', or null will always evaluate to true.
 *
 * So for instance:
*/

'iamateapot' == true; // true
'false' == true; // true, this is true because it's a string of false not a boolean of false;
 0 == true; // false;

In your particular case you are doing

$this->getRow()->discountPercent ? false : true;

Which is doing a loose comparison, my preference is to always specify what you are comparing against, so your first example would be what I would personally choose.

Upvotes: 0

Related Questions