Virb
Virb

Reputation: 1578

'If' Condition going wrong

Here, I am checking condition if $type="register" then it should not go in if condition as I did it like below but it goes wrong.

Here I am passing $type="register" but it is going in if condition and echoing 1.

if((isset($type)) && (($type != "register") || ($type != "document_approved") || ($type != "document_rejection")))
{
    echo 1;
}
else
{
    echo 2;
}

But if I check condition like below then it is working fine and echoing 2.

if((isset($type)) && ($type != "register"))
{
    echo 1;
}
else
{
    echo 2;
}

Am I going wrong anything? Thanks.

Upvotes: 0

Views: 75

Answers (4)

meignanamoorthy ks
meignanamoorthy ks

Reputation: 54

Try Like this,

if((isset($type))
 {
   if(($type != "register") && ($type != "document_approved") && ($type != "document_rejection"))
         {
           echo 1;
         }
         else
         {
           echo 2;
         }
   }

Upvotes: 1

aenugula karthik
aenugula karthik

Reputation: 359

try like this:

if((isset($type)){
    if(($type != "register") || ($type != "document_approved") || ($type != "document_rejection"))
         {
           echo 1;
        }else{
           echo 2;
            }
   }

Upvotes: 0

shubham johar
shubham johar

Reputation: 278

You if condition contains

(($type != "register") || ($type != "document_approved") || ($type != "document_rejection"))

This will evaluate to true when atleast one condition is matched.

Since $type = 'register', so $type != "document_approved" evalutates to true and it prints 1

Upvotes: 3

exussum
exussum

Reputation: 18576

The logic is below

False ( it is equal to register)

True(it's not equal to document approved)

This check won't go any further.

Look at using !in_array($value, [register, document_approved,etc])

Upvotes: 1

Related Questions