pepe
pepe

Reputation: 9919

PHP XOR - how to use with if( )?

I wondered if instead of

function edit_save($data, $post_id, $post_type)
{
    if (($post_type == 'A') OR ($post_type == 'B')) {

            // do this

    }

I could use XOR as below -- but it does this not work...

function edit_save($data, $post_id, $post_type)
{
    if ($post_type == 'A' XOR 'B') { // pseudo code

            // do this

    }

Any suggestions how to simplify the overall syntax and present the 2 potential options for a variable within a conditional statement?

Upvotes: 2

Views: 3656

Answers (3)

Mchl
Mchl

Reputation: 62395

Seems like what you want is actually this:

if (in_array($post_type,array('A','B'))) { 
   ...
}

Which in PHP 5.4+ will look even nicer:

if (in_array($post_type,['A','B'])) { 
   ...
}

Upvotes: 2

Madara's Ghost
Madara's Ghost

Reputation: 175098

Try:

function edit_save($data, $post_id, $post_type)
{
    if (($post_type == 'A') XOR ($post_type == 'B')) {

            // do this

    }

The second statement ('B') in your code will always return true. You need the full conditional statement for it to work.

Upvotes: 6

tomsseisums
tomsseisums

Reputation: 13377

Uhm?

function edit_save($data, $post_id, $post_type)
{
    if (($post_type == 'A') XOR ($post_type == 'B')) {

            // do this

    }

Upvotes: 2

Related Questions