2012-06-29 26 views
3

我們有一個「gotItem」函數,如果在「on_hand」值爲「1」的Item上運行,還應該調用「outItem」函數。出於某種原因,無論「on_hand」值是「1」還是「0」,「gotItem」函數都會調用「outItem」函數。任何想法爲什麼?代碼如下從另一個PHP函數調用PHP函數的邏輯錯誤

function gotItem($user_item_id, $user_id) 
{ 
    $user_item = $this->getUserItem($user_id, $user_item_id); 
    if ((! is_null($user_item)) && ($user_item['on_hand'] = '1')) 
    $this->outItem($user_item_id, $user_id); 

回答

3

=用於分配使用==檢查

function gotItem($user_item_id, $user_id) { 
    $user_item = $this->getUserItem($user_id, $user_item_id); 
    if ((! is_null($user_item)) && ($user_item['on_hand'] == '1')) 
    $this->outItem($user_item_id, $user_id); 
+0

優秀。它做到了。謝謝。 – user1263003

3

您的比較是錯誤的:

$user_item['on_hand'] = '1' 

應該是:

$user_item['on_hand'] == '1' 

由於這項任務,第一個將永遠是真理。可悲的是這是一個非常常見的錯誤,但你可以寫這樣的條件:

'1' = $user_item['on_hand'] 

這將導致一個錯誤,因爲你不能使用文字作爲左手錶達。立即表示代碼中出現錯誤:)

+0

+ 1爲解決一個很常見的問題,往往容易錯過「作者」問題 – Havelock

+0

優秀。它做到了。謝謝。 – user1263003