Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP XOR - how to use with if( )?

Tags:

php

boolean

xor

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?

like image 874
pepe Avatar asked Oct 06 '26 23:10

pepe


1 Answers

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.

like image 82
Madara's Ghost Avatar answered Oct 09 '26 13:10

Madara's Ghost



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!