Scott B
Scott B

Reputation: 40187

What's the logic flaw in this conditional?

I've created this code branch so that if the permalink settings do no match at least one of the OR conditions, I can execute the "do something" branch. However, I believe there is a flaw in the logic, since I've set permalinks to /%postname%.html and it still tries echo's true;

I believe I need to change the ORs to AND, right?

if (get_option('permalink_structure') !== "/%postname%/" || 
          get_option('my_permalinks') !== "/%postname%/" || 
    get_option('permalink_structure') !== "/%postname%.html" || 
          get_option('my_permalinks') !== "/%postname%.html"))
{
//do something
    echo "true";
}

Upvotes: 0

Views: 257

Answers (6)

Felix Kling
Felix Kling

Reputation: 816790

As the other say, use AND (&&) instead of OR (||). If you are unfamiliar with Boolean logic, here in short:

TRUE and FALSE == FALSE
TRUE and TRUE == TRUE

FALSE or FALSE == FALSE
TRUE or FALSE == TRUE

Upvotes: 0

Joe Phillips
Joe Phillips

Reputation: 51160

if !(get_option('permalink_structure') == "/%postname%/" || 
           get_option('my_permalinks') == "/%postname%/" || 
     get_option('permalink_structure') == "/%postname%.html" || 
           get_option('my_permalinks') == "/%postname%.html"))
{
    echo "true";
}

Upvotes: 0

Kevin
Kevin

Reputation: 13226

Use ands, not ors. Have made the same mistake myself.

Upvotes: 0

Toad
Toad

Reputation: 15935

when you do a construct like this:

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

then it will always be true since for this to be false $a should be 1 and 2 simultaneously.

your construct is similarly flawed.

Upvotes: 4

Pascal MARTIN
Pascal MARTIN

Reputation: 401142

You are testing :

if(get_option('permalink_structure') !== "/%postname%/"

And your permalink is /%postname%.html -- which means it is not "/%postname%/"

So, that first portion of the condition is true, and you are entering into the if block -- and the other ones are not even evaluated.


I suppose what you want is to use &&, and not || :

if(get_option('permalink_structure') !== "/%postname%/" 
    && get_option('my_permalinks') !=="/%postname%/" 
    && get_option('permalink_structure') !== "/%postname%.html"
    && get_option('my_permalinks') !=="/%postname%.html"))
{
//do something
    echo "true";
}

Which would mean :

  • if permalink is not "/%postname%/"
  • and permalink is not "/%postname%/"
  • and permalink is not "/%postname%.html"
  • and permalink is not "/%postname%.html"
  • then, echo true

Upvotes: 4

St. John Johnson
St. John Johnson

Reputation: 6660

Yes, switch them to AND and it should work. You could instead reverse those to == and put a ! in the front.

Upvotes: 0

Related Questions