Don1
Don1

Reputation: 39

PHP IF Statement - Two Variables Not Working

I have an IF statement and I need to check two variables, then execute the action if either of the variables are false.

I know I can use the OR (||) statement however for some reason it is not working. I am in the process of learning PHP so it's probably a syntax error on my behalf.

If I create the if statement with only one (either) variable, it works fine:

<?php if ($var1 == false) { do whatever...} ?>

However when I try to check two, neither of the variables seem to be checked. I have tried different syntax variations but nothing works:

<?php if (($var1 == false) || ($var2 == false)) { do whatever...} ?>
<?php if (($var1 == false) OR ($var2 == false)) { do whatever...} ?>
<?php if ($var1 == false || $var2 == false) { do whatever...} ?>
<?php if ($var1 == false OR $var2 == false) { do whatever...} ?>

Can someone please point out what my error is?

Thanks!

EDIT: Including the actual code.

<?php $member = $members_template->member; $bpmember = bp_get_member_user_id(); ?>

                <?php $membersearchinclude = xprofile_get_field_data( 'Exclude yourself from website search results?', $bpmember ); ?>
                <?php $adminsearchinclude = xprofile_get_field_data( 'Exclude From Search Results', $bpmember ); ?>
                <?php if (($adminsearchinclude == false) || ($membersearchinclude == false)) { ?>

This is extracting the xprofile field state from two different checkboxes in BuddyPress. I am checking if either of the checkboxes are false, then executing code.

Upvotes: 1

Views: 706

Answers (2)

Don1
Don1

Reputation: 39

I have managed to solve the issue. It seems my logical operators needed changing:

if ((!$adminsearchexclude) && (!$membersearchexclude))

I am sure if I posted the entire code one of you would have got it. This is a sensitive project at the moment at unfortunately posting the entire code wasn't an option.

Many thanks to everyone that chipped in especially @ChrisO'Kelly for the var dump snippets. You got me thinking in the right direction and I now have a new tool in my arsenal :)

THANK YOU ALL!!

Upvotes: 1

rioastamal
rioastamal

Reputation: 75

I'm not sure what causing it to not run as expected because from my view its all valid. Try to post your code so we know the whole logic.

Try to use strict comparison.

$var1 = 0;
$var2 = 1;

// The echo will not be executed
if ($var1 === false || $var2 === false) {
    echo "Strict comparison\n";
}

// The echo will be executed
if ($var1 == false || $var2 == false) {
    echo "No strict comparison\n";
}

References:

http://php.net/manual/en/language.operators.logical.php http://php.net/manual/en/language.operators.comparison.php

Upvotes: 0

Related Questions