The statement returns false if it shouldn't

The following statement returns false

for whatever reason:

$coin = isset($_GET['market']) ? $_GET['market'] : 'BTC_USD';
echo $coin;
if($this->model->coins($coin) == false):
    $coin = 'BTC_USD';
else:
    $coin = $_GET['market'];
endif;

      

Model:

public function coins($coin)
{   
    $coins = array("BTC_USD","BTC_GBP","LTC_USD","LTC_GBP","BTC_LTC","USD_GBP");
    if (!in_array($coin, $coins))
    {
       return false;
    }
}

      

If i echo

$coin

before the operator if

returns the correct coin, but after the operator it if

returns false

. I know this is a simple solution, it just completely bypasses me. :(

+3


source to share


1 answer


The method coins()

never returns true

. When it doesn't return false

, it returns undefined

because there is no alternative operator return

. Since you are performing a free comparison to the caller undefined == false

. Change the method to:



public function coins($coin)
{   
    $coins = array("BTC_USD","BTC_GBP","LTC_USD","LTC_GBP","BTC_LTC","USD_GBP");
    return in_array($coin, $coins));
}

      

+5


source







All Articles