0
votes

In PHP, how can I not execute coming up code (not displayed in a div for example), if three conditions are true, and on the other hand, do execute it, if and only if any of these 3 conditions is false?

For example, the opposite of what I want to do is, in order to do execute code if the three conditions are indeed true, I do something like this:

<?php
$var1 = 'true';
$var2 = 'true';
$var3 = 'false';
?>

<?php if ( $var1 == 'true' and $var2 == 'true' and $var3 == 'true' ) : ?>
    <?php echo 'All 3 are true, will execute code.'; ?>
<?php else : ?>
    <?php echo 'Not all 3 are true, will not execute code.'; ?>
<?php endif; ?>

Result: Not all three are true, thereby the code will not get executed.

Hope I was understood, Thanks in advance.

6
It's not php, it's logicdev-null-dweller
Why are you using strings instead of booleans?Colin Brock
To clarify, the code should execute if any of the variables is false, and should not execute if all of the variables are true? What about the in-between scenarios that meet neither of these criteria? And is there any reason you're using true and false as string values ('true') rather than booleans (true)?Mitya

6 Answers

1
votes

You can use DeMorgan's Laws to invert the truth of your whole if clause:

if( $var1 != true or $var2 != true or $var3 != true ){
    //execute code
}

Or you can do that with the ! operator, which is completely logically equivalent:

if( !( $var1 == true and $var2 == true and $var3 == true ) ){
    //execute code
}

Or you can invert the values that you check for, which is also logically equivalent:

if( $var1 == false or $var2 == false or $var3 == false ){
    //execute code
}

All of these will work the same way. If you study them carefully, you may also understand WHY they all work the same way, which will help you design these clauses in the future!

1
votes
if (!($x=='x' && $y=='y' && $z=='z')){
// do if matches
}else {
//do otherwise
}

You just put a ! sign like if(!())

1
votes

The easisest thing would probably be to use ! (not) operator as below:

<?php if (!( $var1 == 'true' and $var2 == 'true' and $var3 == 'true' ))  ?>
    <?php echo 'Not all 3 are true, will execute code.'; ?>
<?php else : ?>
    <?php echo 'All 3 are true, will execute code.'; ?>
<?php endif; ?>

or alternatively "open up" the ! (not) operator:

<?php if $var1 != 'true' or $var2 != 'true' or $var3 != 'true'   ?>
    <?php echo 'Not all 3 are true, will execute code.'; ?>
<?php else : ?>
    <?php echo 'All 3 are true, will execute code.'; ?>
<?php endif; ?>
0
votes

There's a != operator which is the opposite of the == operator, it returns false if the two things are not equal. There's also a ! operator that returns the opposite boolean value of the thing it is modifying, such as

!($var1 == 'true' or $var2 == 'true')

returns false if both $var1 and $var2 are 'true'.

Additionally, you don't need to open and close your <?php ?> tag for every individual line of php. In fact don't do that it makes reading your code extremely annoying. Just do a <?php where you start your php and ?> where you end.

0
votes
<?php
$var1 = true;  // IF these are really BOOLEAN values
$var2 = true; 
$var3 = false;
?>

<?php if ( $var1 && $var2 && $var3 ) : ?>
    <?php echo 'All 3 are true, will execute code.'; ?>
<?php else : ?>
    <?php echo 'Not all 3 are true, will not execute code.'; ?>
<?php endif; ?>
0
votes

Try this:

<?php if($var1 == 'true' and $var2 == 'true' and $var3 == 'true'): ?>
   <?php echo 'All 3 are true, will execute code.'; ?>
<?php elseif($var1 != 'true' and $var2 != 'true' and $var3 != 'true'): ?>
   <?php echo 'Not all 3 are true, will not execute code.'; ?>
<?php endif; ?>