2016-09-18 68 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

是的這是!我的問題,我從來不知道或看過整個命令,謝謝。 –