2016-09-18 67 views
1

我做了基于一对夫妇的PHP脚本‘如果’的语句。$ depOption只能是bitcoinethereumliskEURUSD“ELSEIF”不工作

if声明内容作品,然而在ELSEIF语句的内容返回$VAR为0

我自己测试了这些语句的代码,并且他们的工作。只有当我把它们放在我ELSEIF声明,他们不工作。

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."); 
    } 

回答

3

这条线是不正确的:

if ($depOption == "bitcoin" or "ethereum" or "lisk") 

它解析为,如果你写:

if (($depOption == "bitcoin") or "ethereum" or "lisk") 

由于"ethereum"是truthy,该or表达式返回true,无论$depOption值。写这个正确的方法是:

if ($depOption == "bitcoin" or $depOption == "ethereum" or $depOption == "lisk") 
+0

常见的选择是'如果(in_array($ depOption,阵列( “比特币”, “复仇”, “lisk”))'... – nogad

+0

是的这是!我的问题,我从来不知道或看过整个命令,谢谢。 –