Andy Holmes
Andy Holmes

Reputation: 8047

PHP if statement conundrum

First of all, I understand why this is not working, but am not 100% sure on a decent way to fix it within my scenario.

I have 2 arrays, male genetics and female genetics. There's a loop that runs through and pairs them together, here's an example:

'Bell-Albino' => 'Bb'

Problem is, I need to do something like this:

if($gene != 'BB' || $gene != 'RR' || $gene != 'TT'){
    echo 'Recessive Albino';
}

The conundrum is, if the $gene is BB for example, this will always return 'recessive albino' because the other 2 or statements are being matched.

The array has many key's and this code only needs to run on the above values, hence why I can't do a simple check on if they value is upper case. EE isn't an albino strain, only BB, RR and TT are.

I could do this as an elseif() condition but it would add an extra 10 lines or so and not sure if there is a better way.

Upvotes: 0

Views: 62

Answers (1)

Victor Smirnov
Victor Smirnov

Reputation: 3780

The question is slightly confusing. But let me try to guess.

I think you are wanting to write something like this

$gene = 'Bb';
if (!in_array($gene, ['BB', 'RR', 'TT'])) {
    echo 'Recessive Albino'.PHP_EOL;
} else {
    echo 'Non Recessive Albino'.PHP_EOL;
}

If $gene is not in the given list we have recessive albino. Else we have may be non recessive albino.

Initially I suggested to use strtoupper but it seems like this is wrong and the case do make difference.

Upvotes: 2

Related Questions