有一个有趣的问题。在一些遗留代码中,我们有以下语句。
if (empty($result['email_address']) && empty($result['mobile_number'])) {
$token = '';
} else {
$tokenFinder = new TokenFinder();
$tokenFinder->setEmailAddress($result['email_address']);
$tokenFinder->setMobileNumber($result['mobile_number']);
$token = $tokenFinder->generate();
}
令牌取景器中的相关内容如下所示:
class TokenFinder{
public function setEmailAddress($email) {
$this->email = $email;
}
public function setMobileNumber($mobile) {
$this->mobile = $mobile;
}
public function generate(){
if ($this->email == '' && $this->mobile == ''){
Throw new Exception('TokenFinder: You cannot fetch a token with no email or mobile number');
}
昨天,有史以来第一次,在generate()
方法异常被触发。我通过这段代码运行了失败消息中的所有收件人,并且异常不会触发。自抛出Exception以来数据没有改变。这是一个奇怪的。
有谁知道任何会导致empty($var)
评估为false的值,并且$var == ''
将评估为true。
http://stackoverflow.com/questions/25100890/in-php-why-empty0-returns-true – fizzi
这样的问题很好的例子,为什么总是使用'==='而不是'=='像PHP和JavaScript等语言,以确保您检查严格的平等。 – Simba
可能重复[在PHP中,为什么空(“0”)返回true?](http://stackoverflow.com/questions/25100890/in-php-why-empty0-returns-true) – DarkBee