Jump to content

if ((a != 1) || (a != 2)) problem


zendoctor

Recommended Posts

Maybe i've spent too long looking at the screen today but I have a silly little error which is stopping me from completing a solution.

 

Basically I want to say 'if ID is not 301 or 302 do this'.  Here is my code:

if (($this->phone_id_response[PHONE_ID_PHONE_TYPE] != '301') || ($this->phone_id_response[PHONE_ID_PHONE_TYPE] != '302')) { // is NOT a Landline or Contract Mobile
			switch($this->phone_id_response[PHONE_ID_PHONE_TYPE]) {
				case 300: 	$phone_type = "Undetermined";
							break;
				case 301: 	$phone_type = "Landline";  // Shouldn't ever display this, for debug only
							break;
				case 302: 	$phone_type = "Contract Mobile";  // Shouldn't ever display this, for debug only
							break;
				case 303: 	$phone_type = "Pre Paid Mobile";
							break;
				case 304: 	$phone_type = "Toll Free";
							break;
				case 305: 	$phone_type = "Non-Fixed VOIP";
							break;
				case 306: 	$phone_type = "Pager";
							break;
				case 307: 	$phone_type = "Payphone";
							break;
				case 308: 	$phone_type = "Invalid";
							break;
				case 309: 	$phone_type = "Restricted Number";
							break;
				case 310: 	$phone_type = "Personal";
							break;
				case 311: 	$phone_type = "Voicemail";
							break;			
			}
			echo "Sorry, phone type " . $phone_type . " is not allowed.  Please use a Land Line or Contract Mobile Phone";
			exit;
}

 

If the value is 301 it's still giving an error (hence adding those items to the switch statement for debug).  If I remove the or section it evaluates correctly for a single value so there isn't a problem with the variable.

 

Can anyone please tell me what i've got wrong in the if statement?

 

I've tried using 'or' and without the nested brackets both gave the same result.

 

The other option is to rewrite the code to put the error message into each relevant line of the switch statement but i'd rather understand why it isn't working.

 

Thanks,

 

Matt

 

Link to comment
https://forums.phpfreaks.com/topic/197556-if-a-1-a-2-problem/
Share on other sites

You should be using the AND operator not OR for that. For example

 

if (($this->phone_id_response[PHONE_ID_PHONE_TYPE] != '301') && ($this->phone_id_response[PHONE_ID_PHONE_TYPE] != '302')) 

 

In the above line, the switch statement will only be executed if the ID is not 301 AND not 302. If you use OR like you have, then it makes perfect sense that the switch is being executed because you're only saying the ID needs to be not 301 OR not 302. If the ID is 301 then not 301 =  false but not 302 = true so the code inside the if statement is executed.

 

Hope that make some sense!

Link to comment
https://forums.phpfreaks.com/topic/197556-if-a-1-a-2-problem/#findComment-1036825
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.