2013-11-15 64 views
0

我的php在if語句的某處肯定是錯的。出於某種原因,它只會在數組中以「歷史類型」的形式返回「購買」,即使原始數據以其他方式顯示我也是如此。我做錯了什麼?php if語句只返回第一個「if」

$history = $api->get_wallet_history('USD'); 

$i = 0; 
while ($i <= 9): 
$history_date = $history[result][$i][Date]; 
//$history_date->format("m-d-y"); 
//print_r($history_date); 

$history_type = $history[result][$i][Type]; 
if($history_type = 'spent'): 
    $history_type = 'Buy'; 
    elseif($history_type = 'earned'): 
     $history_type = 'Sold'; 
    elseif ($history_type = 'fee'): 
     $history_type = 'Fee'; 
    else: 
     $history_type = 'Error'; 
endif; 
//print_r($history_type); 

$history_usd = $history[result][$i][Balance][value]; 
$history_btc = $history[result][$i][Trade][Amount][value]; 
$history_amt = $history[result][$i][Value][value]; 
echo '<tr><td>'.$history_type.'</td><td>'.$history_amt.'</td><td>'.$history_usd.'</td><td>'.$history_btc.'</td><td>'.$history_date.'</td></tr>'; 
$i++; 
endwhile; 
+1

值請記住,比較字符串時,請使用'=='而不是'='。 PHP中的'='是賦值運算符。 – Ruly

+0

爲什麼不使用'{}'... – aldanux

+0

謝謝,即使在「If」語句中我也沒有意識到這是真的。 我還以爲==意思是「絕對等於」。即,如果($ history_type「絕對等於」「花」): 其現在已經很清楚。感謝大家的解釋。 –

回答

2

$history_type = 'spent'必須$history_type == 'spent'

Assigments總是返回分配新建分配FY值。 $history_type = 'spent'返回'已用',其解釋爲true

if必須看起來

if ($history_type == 'spent'): 
    $history_type = 'Buy'; 
elseif ($history_type == 'earned'): 
    $history_type = 'Sold'; 
elseif ($history_type == 'fee'): 
    $history_type = 'Fee'; 
else: 
    $history_type = 'Error'; 
endif; 

Assignment OperatorsComparison Operators

爲了避免這種錯誤,你可以改變的價值和可變的地方可能。

'spent' = $history_type - 永遠不會奏效 'spent' == $history_type - 按預期工作

+0

很棒的竅門。謝謝! –

0

您正在分配的,而不是在這裏進行比較:

if ($history_type = 'spent'): 

這就是爲什麼它總是有 '花'