2013-03-12 232 views
1

我有這樣的代碼:PHP邏輯運算符IF語句

if (($oldTime < (time() - self::wait))) { 
    if ($this->setTime()) 
    { 
    return true; 
    } 
    else 
    { 
    return false; 
    } 
} else { 
    return false; 
} 

我可以將其替換爲:

if (($oTime < (time() - self::wait)) && $this->setTime()) { 
    return true; 
} else { 
    return false; 
} 

我需要它來檢查,如果$this->setTime()返回true僅當$oTime < (time() - self::wait)是真實的。

+1

是的,你可以,這是一樣的。 – dfsq 2013-03-12 19:56:56

+1

* Offtopic *老兄,我們擁有相同的頭像......尊重我的權威! :-D – Quasdunk 2013-03-12 20:04:58

+0

@Quasdunk哈哈) – rinchik 2013-03-12 20:13:32

回答

6
return ($oTime < (time() - self::wait)) && $this->setTime() 
+0

所以我可以更換權利?如果'$ oTime <(time() - self :: wait)'爲false,'$ this-> setTime()'不會被執行? – rinchik 2013-03-12 19:58:35

+0

是的,如果我正確理解你。 &&是一個短路操作符,所以如果第一個條件是錯誤的,第二個條件將不會執行。 – 2013-03-12 19:59:45

1

是的,你可以使用這個

if (($oTime < (time() - self::wait)) && $this->setTime()) { 
     return true; 
    } else { 
     return false; 
    } 
1

如果if語句中第一個條件與& &(未||)失敗,它會去else分支自動未經覈實的第二個條件