Compare the same variable in PHP [closed]

3

I have the same variable where it can take two values

<?php if($adm1){
      $email = '[email protected]';
}elseif( $adm1){
      $email = '[email protected]';
}

How to compare the two variables as follows, if anything that comes different from these two emails I should issue an alert

I thought the following way:

<?php if($email !='[email protected]' OR $email !='[email protected]'){
        echo 'alerta aqui';

}

But even if email falls into one of these conditions, it will always return wrong;

The fact is that you wanted to issue a warning whenever the email is different from these two options.

    
asked by anonymous 06.04.2016 / 16:20

3 answers

1

One way I use whenever a variable can take multiple values is:

$possibilidades = array(
    '[email protected]',
    '[email protected]',
);

if(in_array($email, $possibilidades)){
    // O valor assumir alguma das possibilidades
}

if(!in_array($email, $possibilidades)){
    // O valor não assumir nenhuma das possibilidades
}

The in_array can be used as a || for several possibilities. Also facilitating the insertion of new possibilities.

  • In your case, just use the second type, which has !in_array .
06.04.2016 / 16:41
3

Test like this, with the && comparator operator:

if ($email !='[email protected]' && $email !='[email protected]') {
    echo 'alerta aqui';
}

Or, using array, you can enter as many emails as you want:

if (!in_array($email, array('[email protected]', '[email protected]'))) {
    echo 'alerta aqui';
}

I hope it helps.

    
06.04.2016 / 16:30
2

I think this is what you want:

if ($email !='[email protected]' && $email !='[email protected]') {

This says both should be different.

or

if (!($email =='[email protected]' || $email =='[email protected]')) {

Here it says that any one that is equal to these values should NOT execute the block.

I do not understand what the goal is, I do not understand what you really expect.

    
06.04.2016 / 16:27