Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Statement is returning false when it shouldn't

Tags:

php

The following statement returns false for some 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 the $coin before the if statement it returns the correct coin, but after the if statement it's returning false. I know it's an easy fix, it's just bypassing me completely. :(


1 Answers

The coins() method never returns true. When it doesn't return false, it returns undefined because there's no alternative return statement. Since you're doing a loose comparison in 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));
}
like image 71
Barmar Avatar answered Dec 07 '25 22:12

Barmar



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!