Whyte the Weeabear Whyte the Weeabear - 2 months ago 13
PHP Question

"Elseif' not working

I've made a php script based on a couple of "If" statements. $depOption can only be

bitcoin
,
ethereum
,
lisk
,
EUR
or
USD
.

Currently I am having some problems. The content in the
if
statement works, however the content in the elseif statements return
$VAR
as 0. What's wrong? I've tested the code in these statements by themselves, and they work. Only when I put them in my elseif statements, they don't work.

Thanks in advance for the help.

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

Answer

This line is incorrect:

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

It's parsed as if you'd written:

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

Since "ethereum" is truthy, the or expression returns true, regardless of the value of $depOption. The correct way to write this is:

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