Jump to content

Recommended Posts

Hey guys,

 

Just been playing around with some IF OR arrays and i was wondering if you could shed some light. The following code:

 

if ($arrayTextPlain[1] != "$registrationID" . "1" || $arrayTextPlain[1] != "$registrationID" . "2") {

//reg id doesn't match			  

} else {

//reg id matches
}

 

Returns false. I.e the reg ID's don't match. However, if i switch them around:

 

if ($arrayTextPlain[1] != "$registrationID" . "2" || $arrayTextPlain[1] != "$registrationID" . "1") {

//reg id doesn't match			  

} else {

//reg id matches
}

 

It returns true. The reg ID's do match. Shouldn't the first one work as well? Isn't that the point of an OR statement? Or have i just not got the syntax correct..?

 

(The reg ID's in the array are meant to match with . "1" and . "2" btw)

 

Thanks lads.

Link to comment
https://forums.phpfreaks.com/topic/53416-if-or-statements/
Share on other sites

$registrationID = $_POST['registrationID']; - The input from a form

$arrayTextPlain being an array that i have outputted the contents of a file to. $arrayTextPlain[1] being a line in the array.

 

Both are defined just above the IF OR statement.

 

But i mean, it's more the logic and syntax i am confused about. I know for a fact the variables i am comparing match. So theoretically my first IF OR statement should work. Not just the second one.

 

Edit/ sorry for bump.

Link to comment
https://forums.phpfreaks.com/topic/53416-if-or-statements/#findComment-263971
Share on other sites

Let's do some testing shall we?

 

<?php
if ($arrayTextPlain[1] != $registrationID . "2" || $arrayTextPlain[1] != $registrationID . "1") {
    if ($arrayTextPlain[1] != $registrationID . "2") {
           echo 'Test1: ' . $arrayTextPlain[1] . ' did eqaul ' . $registrationID . '2<br />';
    }else {
           echo 'Test1: ' .$arrayTextPlain[1] . ' did NOT eqaul ' . $registrationID . '2<br />';
    }

    if ($arrayTextPlain[1] != $registrationID . "1") {
           echo 'Test2: ' .$arrayTextPlain[1] . ' did eqaul ' . $registrationID . '1<br />';
    }else {
           echo 'Test2: ' .$arrayTextPlain[1] . ' did NOT eqaul ' . $registrationID . '1<br />';
    }

    if ($arrayTextPlain[1] != $registrationID . "1" || $arrayTextPlain[1] != $registrationID . "2") {
           if ($arrayTextPlain[1] != $registrationID . "1") {
                echo 'Test3: ' . $arrayTextPlain[1] . ' did eqaul ' . $registrationID . '1<br />';
           }else {
                echo 'Test3: ' .$arrayTextPlain[1] . ' did NOT eqaul ' . $registrationID . '1<br />';
            }

            if ($arrayTextPlain[1] != $registrationID . "2") {
                echo 'Test4: ' .$arrayTextPlain[1] . ' did eqaul ' . $registrationID . '2<br />';
           }else {
                echo 'Test4: ' .$arrayTextPlain[1] . ' did NOT eqaul ' . $registrationID . '2<br />';
           }
    } else {
    echo 'Test3 n 4: ' . $arrayTextPlain[1] . ' did NOT eqaul ' . $registrationID . '1 OR<br />';
    echo 'Test3 n 4: ' .$arrayTextPlain[1] . ' did NOT eqaul ' . $registrationID . '2<br />';
    }
} else {
    echo 'Test1 n 2: ' .$arrayTextPlain[1] . ' did NOT eqaul ' . $registrationID . '2 OR<br />';
    echo 'Test1 n 2: ' .$arrayTextPlain[1] . ' did NOT eqaul ' . $registrationID . '1<br />';
}
?>

 

See what prints out and see if there is a reason why it would not be working.

Link to comment
https://forums.phpfreaks.com/topic/53416-if-or-statements/#findComment-263990
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.