Compare the same variable in PHP

Asked

Viewed 940 times

3

I have the same variable where it can assume two values

<?php if($adm1){
      $email = 'fulano@email.com';
}elseif( $adm1){
      $email = 'ciclano@email.com';
}

How to compare the two variables as follows, if anything other than these two emails I should issue an alert

I thought as follows:

<?php if($email !='fulano@email.com' OR $email !='ciclano@email.com'){
        echo 'alerta aqui';

}

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

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

  • 1

    I don’t understand what your difficulty is. You don’t know which operator is used to OR? You want a AND?

  • Sorry at the beginning of PHP. What I need is to compare the values where $email always takes a single value and what I need is to validate if the value it received is different from the two options I mentioned, if I send an alert.

  • 2

    This you have already written in the question.

  • 2

    The question that won’t shut up, $email needs to be different from both values or only one of them?

  • 1

    If only one of the values comes different should already issue the alert, I think why complicated

3 answers

3

Test like this with the operator && comparative:

if ($email !='fulano@email.com' && $email !='ciclano@email.com') {
    echo 'alerta aqui';
}

Or, using array, so you can insert as many emails as you want:

if (!in_array($email, array('fulano@email.com', 'ciclano@email.com'))) {
    echo 'alerta aqui';
}

I put in the Github for future reference.

I hope it helps.

2

I think this is what you want:

if ($email !='fulano@email.com' && $email !='ciclano@email.com') {

This says that both must be different.

or

if (!($email == 'fulano@email.com' || $email == 'ciclano@email.com')) {

I put in the Github for future reference.

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

I don’t understand what the point is, I don’t understand what you really expect.

  • The correct is to use the operator && as using || even if the email is equal to one of these two options, it will fall into the if. Hug

  • 1

    Ah, do I have to study logic? I get it. I don’t know what he wants yet. Maybe that’s it, but without it, it complicates.

  • 1

    It wasn’t for you bigdown, @Diego deleted his comment saying that my comment was incorrect, sorry.

  • I did not say that it was incorrect, I just thought that at the end of the day neither the question is clear. Or that the two situations would meet him. All right, it’s not here anymore who commented.

  • @Mastria blz, just for the record, I haven’t denied yours, I haven’t voted for anything here until I have ctz of what he wants.

  • 1

    It is a little confused yes, but it seems to display alert if the email is different from these two, something basic. I also did not understand the negative!

  • The one that makes sense is &&, but his sentence is ambiguous.

  • I also did not deny.

  • I decided to leave only the one that makes sense.

  • Guys, all the help is welcome. But only see if at a given time the $email variable assumes the value 'ciclano@email.com' the condition $email != 'fulano@email.com' will be true and soon will issue the alert, but that way would be wrong, because the variable $email would have the second option of value 'ciclano@email.com'.

  • It’s a silly business, I think, but you’ve tied a knot in my head, it’s purely a matter of logic that I don’t understand

  • 1

    @lima99 The variable cannot have two values, either it has one or it has the other.

Show 7 more comments

1


A mode I use whenever a variable can assume several values is:

$possibilidades = array(
    'fulano@email.com',
    'ciclano@email.com',
);

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 various possibilities.
Also facilitating the insertion of new possibilities.

  • In your case just use the second type, which has !in_array.
  • 1

    You already have a similar answer

  • 1

    Sorry @Mastria here was only 2, and 1 removed, your edit was 8min my answer to 3min, may have taken 5 to type, which would be a "similar thought", do not think?

  • That way it worked. That, a simple thing complicated so much the head. Thank you very much!

  • 1

    Then mark this as correct, to end the confusion...

Browser other questions tagged

You are not signed in. Login or sign up in order to post.