我做了一个基于几个"If“语句的php脚本。$depOption只能是bitcoin、ethereum、lisk、EUR或USD。
if语句中的内容可以工作,但是elseif语句中的内容将$VAR返回为0。
我已经对这些语句中的代码进行了测试,它们可以正常工作。只有当我把它们放在我的其他I语句中时,它们才不起作用。
if ($depOption == "bitcoin" or "ethereum" or "lisk")
{
// Get information on altcoin values
$request = 'https://api.coinmarketcap.com/v1/ticker/';
$response = file_get_contents($request);
$data = json_decode($response, true);
$price = null;
foreach ($data as $item) {
if ($item["id"] == "$depOption") {
$VAL = $item["price_usd"];
break;
}
}
}
elseif ($depOption == "EUR")
{
// Get EUR exchange rate
$eurrequest = 'http://api.fixer.io/latest';
$eurresponse = file_get_contents($eurrequest);
$eurdata = json_decode($eurresponse, true);
$VAL = $eurdata['rates']['USD'];
}
elseif ($depOption == "USD")
{
$VAL = 1;
}
else
{
die("Something went wrong.");
}发布于 2016-09-19 07:59:23
此行不正确:
if ($depOption == "bitcoin" or "ethereum" or "lisk")它就像你写的那样被解析:
if (($depOption == "bitcoin") or "ethereum" or "lisk")因为"ethereum"是真的,所以or表达式返回true,而不管$depOption的值是什么。正确的写法是:
if ($depOption == "bitcoin" or $depOption == "ethereum" or $depOption == "lisk")https://stackoverflow.com/questions/39563696
复制相似问题