djb2002 Posted May 23, 2020 Share Posted May 23, 2020 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. Quote Link to comment Share on other sites More sharing options...
Barand Posted May 23, 2020 Share Posted May 23, 2020 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 ) ) 1 Quote Link to comment Share on other sites More sharing options...
djb2002 Posted May 23, 2020 Author Share Posted May 23, 2020 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 ? Quote Link to comment Share on other sites More sharing options...
requinix Posted May 23, 2020 Share Posted May 23, 2020 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. 1 Quote Link to comment Share on other sites More sharing options...
djb2002 Posted May 23, 2020 Author Share Posted May 23, 2020 (edited) 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 May 23, 2020 by djb2002 Quote Link to comment Share on other sites More sharing options...
kicken Posted May 23, 2020 Share Posted May 23, 2020 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) $number = 99 $number != 101 => true $number != 102 => true true || true => true Case 2) $number = 101 $number != 101 => false $number != 102 => true false || true => true Case 3) $number = 102 $number != 101 => true $number != 102 => false true || false => true What you want is AND, not OR. Case 1) $number = 99 $number != 101 => true $number != 102 => true true && true => true Case 2) $number = 101 $number != 101 => false $number != 102 => true false && true => false Case 3) $number = 102 $number != 101 => true $number != 102 => false true && false => false 1 Quote Link to comment Share on other sites More sharing options...
djb2002 Posted May 23, 2020 Author Share Posted May 23, 2020 Thank you so much for all the replies and explanations - That makes perfect sense. I think was one of those things. The more time I spent looking the harder I could find it. All working great now - Thanks again. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.