2013-11-15 66 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'): 

这就是为什么它总是有 '花'