tags:

views:

161

answers:

6

I think sometimes I don't need to use ELSEIF and not repeat stuff by maybe doing multiple checks inline.

I've done this before.

I'm trying this simple line and just can't make it work.

I read that:

OR is the same as ||

AND is the same as &&

Code (The short way, but doesn't work):

if($subdom<>'www' or $subdom<>'nosub')
{do some stuff if any of the two are meet}

I also tried this:

Code:

if((subdom!='www') || ($subdom!='nosub')){}

What will be the correct operators to check for any of the two? Is it || or &&. Should we use != or <> also?

Some Info found after question answered:

$a && $b
//And //TRUE if both $a and $b are TRUE.

$a || $b
//Or //TRUE if either $a or $b is TRUE.

Either of these will work as the comparison:

!== (not equal identical), but not != as it will mean "does not assign"
<> will do the trick without too much merry go round.

+1  A: 

Those operators have the same semantics but a different precedence:

false &&  true or true    // = (false && true) or true  => true
false and true || true    // = false and (true || true) => false
Gumbo
Codex73
monzee
+2  A: 

You have to carefully think through the logic that you're trying to express.

Right now, your logic says "If the subdomain isn't 'www' or the subdomain isn't 'nosub'". If either one of those is true, the code will be executed.

Let's try some test cases on this:

  • nosub: this isn't "www", so that condition is true, execute the code
  • random: this isn't "www" or "nosub", so both conditions are true, execute the code
  • www: this isn't "nosub", so that condition is true, execute the code

Basically what you've done is made it so everything will be true. What you're actually trying to express is "The subdomain isn't 'www' AND it isn't 'nosub'". That is, if it's NEITHER of those, you want to execute the code.

The correct if statement would be: if ($subd != 'nosub' && $subd != 'www').

If you have trouble with this, it may be better to change your process of coming up with the logic that you put inside the if statement. Here is a description of how you could build it up.

First, start with the condition that you're considering "good". The "good" condition is "$subd is one of 'www' or 'nosub'". The if statement for this would be: if ($subd == 'www' || $subd == 'nosub')

Now, you're trying to write code to handle the condition not being good, so you need to take the opposite of the good condition. To take the opposite of a boolean condition, you surround the whole thing with a NOT. In code: if !($subd == 'www' || $subd == 'nosub') (note the exclamation point in front).

If you want to move that negation inside (as you had been trying to do originally), it's a property of boolean algebra (I hope this is the right term, it's been a long time since I learned this) that when you move a negation into a relation like that, you negate both the operands, and also change the operator from an AND to an OR, or vice versa. So in this case, when you move the negative inside the parentheses, both the == become != and the || becomes &&, resulting in: if ($subd != 'www' && $subd != 'nosub), as I had given above.

Edit again: The properties I mention in the last paragraph are called De Morgan's laws.

Chad Birch
Thank You for your comments.
Codex73
A: 

The if is wrong because all strings will pass the check. All strings are either not 'www' or not 'nosub'. It is for this reason that using == is generally better than !=.

Mohit Chakraborty
+1  A: 

I think you would want to use 'and' rather than 'or'.

Using an 'or' on both of those conditions will always result in true as the string will always not be one of the values.

Jarod Elliott
+4  A: 

Your expression is always true, considering your example, check the table with different input values for $subdom:

$expr = ($subdom!='www') || ($subdom!='nosub');

   $subdom      ($subdom!='www')      ($subdom!='nosub')      result
   ¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯
    www      |      false           |        true         |     true  |
    nosub    |      true            |        false        |     true  |
    anything |      true            |        true         |     true  |
    blah!    |      true            |        true         |     true  |

I think you're wanting to use the AND (&&) operator here...

CMS
Codex73
A: 

I am guessing your passing from your own sub domain something like members, so you need the following

if(($dom == 'www') || ($dom == 'nosub'))
{
    echo 'go away';
}
Andrew Clark