0

I was looking at the PHP hash_equals() function here: https://php.net/manual/en/function.hash-equals.php

I came across the following code:

function hash_equals($a, $b) {
    $ret = strlen($a) ^ strlen($b);
    $ret |= array_sum(unpack("C*", $a^$b));
    return !$ret;
}

What does return !$ret return?

Edit: I know "!" is the logical operator "NOT". However, I wanted to know what it's doing to the return value. Does it return the opposite of the value, or make it false? I read the entire duplicate that this post is linked to, and still don't understand.

Netside
  • 80
  • 2
  • 9
  • And that you don't search forever in the list it's the logical NOT operator. – Rizier123 Jun 07 '15 at 23:17
  • 1
    Whatever value ends up in `$ret`, in that context, the `!` coerces it to a boolean value and inverts it. So truthy values return boolean `false`, and falsy values return boolean `true`. – Michael Berkowski Jun 08 '15 at 23:30
  • 1
    Refer to [PHP's type comparison table](http://php.net/manual/en/types.comparisons.php) for what values are considered true/false. – Michael Berkowski Jun 08 '15 at 23:33
  • Ah, I see. I thought it maybe reversed it, but now that I understand it "coerces" it to a boolean value, it makes sense. – Netside Jun 09 '15 at 21:15

0 Answers0