php : is this if condition correct? - php

I have the following if condition statement
if ( (strlen($data[70])>0) || ( (remove19((trim($data[29])) == '7135556666')) && isLongDistance($data[8])) )
where $data is a recordset from a database.
My goal is to include all rows where $data[70] isn't blank, and also include rows where $data[29] = 713555666 && $data[8] isLongDistance = TRUE
My question is, if isLongDistance($data[8]) returns false, will it still return the row since $data[70] is not blank?
thanks in advance

Yes, because you're doing an OR. Your statement will reduce to "TRUE OR (TRUE AND FALSE)", which will be TRUE.

Yes, because you have an OR operator.

If the first thing returns TRUE then the if will succeed. If first thing will return FALSE, then it will execute the part after OR and if the number is correct and the function returns TRUE, then it will succeed.
Anyway I would overwrite it like this
if ( !empty($data[70]) or ( (int)$data[29] == 7135556666 and isLongDistance($data[8]) )

Yes it is. But to understand realy what it does write code that humans can understand, dont write code just for the computers.
consider this simplefied version.
$dataLenght = strlen($data[70]);
$remove19 = remove19((trim($data[29]);
$longDistance = isLongDistance($data[8]);
if ( $dataLenght > 0 ) // your first OR if false it would goto elseif statement
{
//condition
}
elseif( $remove19 == '7135556666' && $longDistance) // your second statement
{
//conditon
}

Related

What is the meaning of = and === when used in one line or in one variable?

I saw a code showing below.
$selected_value = 'yes' === ( isset( $option['default'] ) ? $option['default'] : 'no' );
I know the difference between = and === and the way how they are used above is new to me. Can someone explain?
If $option['default'] contains "yes" then $selected_value is set to true, otherwise (if it contains something else, or does not exist) it is set to false.
The ternary clause ( condition? true-return-value : false-return-value) either returns "no", or the contents of $option['default']. If that return is equal to yes, then the result of the === comparison will be true, otherwise it will be false.

PHP Is one value set and not another

I'm working on a truthy/falsy where I need to check 2 values, of which one may be null, the other 0. If integer vs string value same, they'd be the same.
The simplest I've been able to break it down is:
if($var1 == $var2) {// They may be the same
if((isset($var1) && !isset($var2)) || (!isset($var1) && isset($var2))) { // Is one set, and not the other?
return true; // As different as we'll allow
}
return false; // Nope they're the same
}
return true; // Definitely different
My question would be mainly, given the above code block, is there a simpler way to check if one value is set, and not the other?
EDIT: The reason I check for equality first, is that the value may pass 1 == 10 or 10 == '10', in which case, I don't need to check further, but if null == 0, this would pass and require further checking. I saw that I can pass multiple values to isset, which would work if I required both to be set or both not set, but if one is, and not the other, that's where I need to flag.
Using the null coalescing operator may come in handy...
return ($var1 ?? null) !== ($var2 ?? null);
Checks that they are not equal and at least one is defined and not null.
You are checking for equality before checking if they are set.
I'd recommend checking if they are set first.
If the PURE goal is to check if one is set and not the other, then:
return ( ( empty($var1) && ! empty($var2) ) || ( ! empty($var1) && empty($var2) );
Per comments, isset is the requirement, so this version:
return ( ( isset($var1) && ! isset($var2) ) || ( ! isset($var1) && isset($var2) );
And, props to #deceze to suggest an even simpler version:
return ( isset($var1) != isset($var2) );

php if and or syntax

I'm struggling to make this statement work
if(((!isset($_COOKIE['email']) && !$_SESSION['remember_me']) || $_SESSION['id']) {
//do something
} else {
//do something
}
I want it to return true if both of the first two statements are met, or if the third statement is met?
You have one too many ( at the start of the line. Other than that, there's no reason why it shouldn't work.
if ( ((!isset($_COOKIE['email'] && !$_SESSION['remember_me'])) || $_SESSION['id'] )
That should work, if i correctly understand what you want

PHP operators if statement 'and' and 'or'

I have an if statement that I want to control with having one field needing input and they have to pick one of the other 2 choices.
if(test1 && test || test3){
//Something here
}
Should I do it like this:
if(test1 && (test2 || test3)){
//do stuff
}
How would I go about doing this. I can't wrap my head around the logic...
if ($requiredField && ($optional1 || $optional2)) {
/* Do something */
}
For the /* Do something */ bit of code to be executed, the if statement has to evaluate to TRUE.
This means, that $requiredField must be TRUE, and so must be ($optional1 || $optional2).
For $requiredField to be TRUE, it just needs to be filled in - and for the second part: ($optional1 || $optional2) either optional1 or optional2 would do it.
Edit:
After rereading the question, it seems that I might have misunderstood you. If the user must enter one specific piece of information, and must choose only one (not both) out of two options - then the following should be used.
if ($requiredField && ($optional1 ^ $optional2)) {
/* Do something */
}
This means that $optional1 or $optional2 must be filled out - but not both of them.
From the sound of it, you want the latter:
if ($test1 && ($test2 || $test3)){
//do stuff
}
Think of it as two conditions needing to be met. This gives you those two conditions. The second condition just happens to be another condition. The first option you posted, however, is quite the opposite as it can allow execution if just $test3 is true
test1 && (test2 || test3) is very easy to understand from the first place - Choose test1 && (test2 || test3) means one the last two. Very clear.
test1 && test || test3 - doesn't seem to be correct:
test1 = false
test2 = false
test3 = true
false && false || true = true
doesn't actually fit your criteria.
... they have to pick one of the other 2 choices
I'm just throwing a guess out here. If you really want to ensure that one, but only one of the two other options are selected, then you need xor:
if ($required AND ($and_either XOR $or_other)) {
You can have 'nested' if statements withing a single if statement, with additional parenthesis.
if(test1 && (test2 || test3)){
//do stuff
}
Your logic is right but your sintax isnt, you should compare the values of the variables as show, or simply ignore them as saying you are trying to compare them as they are TRUE.
$test1=true;
$test2=true;
$test3=false;
if($test1==true && ($test2==true || $test3==true){ echo "YES";}
This will output YES.

Testing if either of two condtions are true

I'm trying to test if either of two variables are true using the code below, but I the code always returns the true conditions even when the variable is blank. Have I done this correctly or is it possible the variables are always true?
Thanks in advance for your help.
<?php
if (($gogo_team_member_twitter !== true) or ($gogo_team_member_facebook !== true)) {
echo('class="amb-with-socal"');
}
else echo('class="amb-without-socal"');
?>
If you need "Testing if either of two condtions are true" then your condition should look like:
if ($gogo_team_member_twitter === true || $gogo_team_member_facebook === true)
or just
if ($gogo_team_member_twitter || $gogo_team_member_facebook)
if you don't need strict comparison
You have the right idea, but you're checking that the variables are not true. Surely you want to check if either is true?
Also, try to use || and && rather than or and and, as they have a higher precedence.
I would just write
if ($gogo_team_member_twitter || $gogo_team_member_facebook)
In addition when dealing with negatives like this you can use "and"
if ($gogo_team_member_twitter !== true && $gogo_team_member_facebook !== true)
Well, you are checking if any of the 2 variables is not exactly equal to true.
If the variable is blank, it is not true, and so the condition is met.

Categories