1

I have this function

//$hasher is a phpass object.    
public function getHash( $check )
    {
    global $hasher;
    if ( $check == 'hash' )
        {
        return $hasher->HashPassword($this->password);
        }
    else if ( $check == 'check' )
        {
        return $hasher->CheckPassword($this->password, $this->getData('data')['password']);
        }
    else
        {
        return 'F*** off';
        }
    }

When i call it this is what i get

$obj->getHash('hash')
//getHash(): $2a$08$Uof.EzLkJI..........

$obj->getHash('check')
//getHash(): 1

$obj->getHash('dsadaldas') //and anything else in the brackets
//getHash():F*** off

$obj->getHash(TRUE)
//getHash(): $2a$08$3vNYnGVsf...

Why does calling the method with TRUE return the same as if i had called it with 'hash' as an argument? Am i missing something here? I tried it with switch() and it still behaves the same.

4

2 回答 2

2

Because a string such as hash evaluates to true when you use the equality operator (==):

You could use the identical (===) operator instead:

if ( $check === 'hash' )

This ensures that both the value of the variable and the type are the same.

于 2013-02-15T12:44:40.397 回答
0

这是因为当您将 boolean ( true) 与 string ( "hash") 进行比较时,字符串将被转换为 boolean,而不是相反。

解决方案:使用类型安全的比较 ( ===)

于 2013-02-15T12:47:41.983 回答