Jump to content

MySQL / PHP - IF/AND/OR Statements - Getting Confused


Recommended Posts

I'm updating some PHP/MySQL code and struggling to get it to work correctly.

At the moment I have the code as follows:

if (strlen($number) < "9" AND $number1 <> "101"){
	
}

else if (strlen($number) > "14") {
}

else {
...........the rest of the code goes here to action after filtering out the above.

 

I want the first if command to match if the length is less than 9 characters long, and the $number1 is either 101 or (I want to add in 102).

I've tried amending the first IF statement to read as follows, but that fails:

if (strlen($number) < "9" AND ($number1 <> "101") OR ($number1 <> "102")){
	
}

I'm expecting it to be something very straightforward, but I've looked at it for so long now I just cannot see it.

Thanks in advance for any help.

 

 

 

When mixing AND and OR in expressions, use parentheses to make the logic explicit. For example, instead of

if (A AND B OR C)

use one of these, depending on what you mean

if ( (A AND B) OR C )

 or

if ( A AND ( B OR C ) )

 

  • Like 1

Thanks for your reply.

I've been trying that and different combinations.  This is what I had changed it to:

<?php
$number = $str;
if (strlen($number) < "9" AND ($number1 <> "101" OR $number1 <> "102")){
}

but that seems to be stopping cases now where $number=101.  Maybe I have got the syntax wrong ?

 

 

 

Think about your logic. If $number is 101 then it <>102, right? And if $number is 102 then it <>101, right?

Also, please do what the rest of the PHP community does: use the && || != operators instead of AND OR <>. The first group is what you see in programming languages. The second group is what you see in SQL. PHP is not SQL.

  • Like 1

Thanks for your reply.

I think I've spent too long thinking about this - Getting me confused.  I understand what you are saying, and that is correct.

I see what you mean - the OR statements are contradicting one another.

Any idea how this should be written ? - I'm wanting it to progress if $number is anything other than 101 or 102 (as well as the length being less than 9).

Thanks again

Edited by djb2002
2 minutes ago, djb2002 said:

but isn't that what my logic reads

The logic $number != 101 OR $number != 102 is always true.  Look at the conditions separately for various cases:

Case 1)

  1. $number = 99
  2. $number != 101 => true
  3. $number != 102 => true
  4. true || true => true

Case 2)

  1. $number = 101
  2. $number != 101 => false
  3. $number != 102 => true
  4. false || true => true

Case 3)

  1. $number = 102
  2. $number != 101 => true
  3. $number != 102 => false
  4. true || false => true

What you want is AND, not OR.

Case 1)

  1. $number = 99
  2. $number != 101 => true
  3. $number != 102 => true
  4. true && true => true

Case 2)

  1. $number = 101
  2. $number != 101 => false
  3. $number != 102 => true
  4. false && true => false

Case 3)

  1. $number = 102
  2. $number != 101 => true
  3. $number != 102 => false
  4. true && false => false

 

  • Like 1
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.