0

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.

8
  • 2
    Could you elaborate on your example? I feel like this probably has a simple solution however I'm not 100% sure what you're after. Commented May 18, 2016 at 7:36
  • Exactly what i want form OP. Commented May 18, 2016 at 7:36
  • Could you post your array's??? Commented May 18, 2016 at 7:44
  • 1
    Shouldn't you just replace || with && ? Commented May 18, 2016 at 7:46
  • 1
    I can't really follow you, to be honest. Of course they're no all set at the same time which is exactly the reason why you need && instead of || when using negative condition checks. Can you give an example of the elseif construct, then we can see how it can be simplified. Commented May 18, 2016 at 8:22

1 Answer 1

2

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.

Sign up to request clarification or add additional context in comments.

3 Comments

The string is output as Bb from using the parental genes. It takes B from the male and b from the female for example and then throws out Bb. So technically it's not in an array. This script is building a punnet square for calculating genetics so it's quite difficult to explain haha
Though as I re read your example this might work how I want, misread it the first time! I'll give it a go and let you know
Thank you, nice clean way :)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.