1

This might sound naive, but in PHP, is it possible modify the outcome of an expression with one boolean value?

for example:

if(expression)
     //execute code

now, how can i do this?

if(!expression)
    //execute code

with the control of another boolean?

like:

$bool = true;
if( ($bool? :!)expression ) // if($bool) => !expresion ; if(!$bool) => expression 
    //execute code

so basically i want to have the have !expression when $bool is true and expression when $bool is false

i do know i can :

$bool ? !expression : expression

but it has the same expression twice...

3 Answers 3

2

So basically you want to pass the test if $bool is true or expression is true, but not both. Textbook definition of XOR:

if( $bool xor expression)
    // execute code
Sign up to request clarification or add additional context in comments.

Comments

1
if (expression == $bool)

or maybe

if (expression != $bool)

Comments

1

I'm not sure i understand you but you can store the result of the expression in variable:

<?php
    $bool = false;
    $result = (1 == 2 || 1 == 1);
    if ($bool && !$result) {
    //do stuff
    echo '1';
    } else if (!$bool && $result) {
    //other stuff
    echo '2';
    } else {
    echo '3';
    }
?>

or maybe

$bool = false;
$result = (1 == 2 || 1 == 1);
$newVar = ($bool ? !$result : $result);

Or use eval but it's a little bit tricky.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.