PHP if 语句只返回第一个“if”


php if statement only returns 1st "if"

我的php在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;

$history_type = 'spent'必须$history_type == 'spent'

吸收

始终返回吸收值。 $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;

赋值运算符、比较运算符

为了避免出现此错误的可能性,您可以更改值和变量的位置。

'spent' = $history_type -- 从不工作 'spent' == $history_type -- 按预期工作

您在此处分配而不是比较:

 if ($history_type = 'spent'):

这就是为什么它将始终具有"花费"的价值