strcmp vs. == vs. === in PHP for checking hash equality

前端 未结 4 1554
暗喜
暗喜 2021-02-19 00:46

I\'m using crypt() to hash passwords in PHP, and am trying to work out the safest way of testing equality of the resulting hash when performing password checks.

4条回答
  •  终归单人心
    2021-02-19 01:11

    That is incorrect, please look at the definition of the function. According to PHP:

    Returns < 0 if str1 is less than str2;

    > 0 if str1 is greater than str2,

    and 0 if they are equal

    It returns less than 0 if str1 is less than str2. Note the phrase "less than", it does not return just -1, but any negative value. The same happens when str1 is greater than str2, but it returns a positive, non-zero value. It returns a positive value that can be 1, or any number thereafter.

    strcmp()returns a number that is the difference between the two strings starting with the last character that was found to be similar.

    Here is an example:

    $output = strcmp("red", "blue");

    The variable $output with contain a value of 16

提交回复
热议问题