Perhaps the double negatives are giving you problems. Let's rewrite it to:
!($country_code == 'example_country_code') || !clientIscrawler()
This can be turned into an equivalent condition with &&:
!($country_code == 'example_country_code' && clientIscrawler())
By reversing the if you would get this:
if ($country_code == 'example_country_code' && clientIscrawler()) {
echo 'skipping';
} else {
echo 'the script can be executed';
}
Therefore, in your code, it will only print skipping if clientIscrawler() is truthy.
Perhaps the double negatives are giving you problems. Let's rewrite it to:
!($country_code == 'example_country_code') || !clientIscrawler()
This can be turned into an equivalent condition with &&:
!($country_code == 'example_country_code' && clientIscrawler())
By reversing the if you would get this:
if ($country_code == 'example_country_code' && clientIscrawler()) {
echo 'skipping';
} else {
echo 'the script can be executed';
}
Therefore, in your code, it will only print skipping if clientIscrawler() is truthy.
If you have multiple conditions with the OR operator, in which case you don't want the if statement to evaluate as true, the syntax is:
if(!($something == "something" || $something == 'somethingelse')){
do stuff...
}
Here is an example:
$apples = array (
1 => "Pink Lady",
2 => "Granny Smith",
3 => "Macintosh",
4 => "Breaburn"
);
foreach($apples as $apple){
// You don't want to echo out if the apple name is "Pink Lady" or "Macintosh"
if(!($apple == "Pink Lady" || $apple == "Macintosh")){
echo $apple."<br />";
}
}
// Output is:
Granny Smith
Breaburn
Videos
Yes. The answer is yes.
http://www.php.net/manual/en/language.operators.logical.php
Two things though:
- Many programmers prefer
&&and||instead ofandandor, but they work the same (safe for precedence). $status = 'clear'should probably be$status == 'clear'.=is assignment,==is comparison.
There's some joking, and misleading comments, even partially incorrect information in the answers here. I'd like to try to improve on them:
First, as some have pointed out, you have a bug in your code that relates to the question:
if ($status = 'clear' AND $pRent == 0)
should be (note the == instead of = in the first part):
if ($status == 'clear' AND $pRent == 0)
which in this case is functionally equivalent to
if ($status == 'clear' && $pRent == 0)
Second, note that these operators (and or && ||) are short-circuit operators. That means if the answer can be determined with certainty from the first expression, the second one is never evaluated. Again this doesn't matter for your debugged line above, but it is extremely important when you are combining these operators with assignments, because
Third, the real difference between and or and && || is their operator precedence. Specifically the importance is that && || have higher precedence than the assignment operators (= += -= *= **= /= .= %= &= |= ^= <<= >>=) while and or have lower precendence than the assignment operators. Thus in a statement that combines the use of assignment and logical evaluation it matters which one you choose.
Modified examples from PHP's page on logical operators:
$e = false || true;
will evaluate to true and assign that value to $e, because || has higher operator precedence than =, and therefore it essentially evaluates like this:
$e = (false || true);
however
$e = false or true;
will assign false to $e (and then perform the or operation and evaluate true) because = has higher operator precedence than or, essentially evaluating like this:
($e = false) or true;
The fact that this ambiguity even exists makes a lot of programmers just always use && || and then everything works clearly as one would expect in a language like C, ie. logical operations first, then assignment.
Some languages like Perl use this kind of construct frequently in a format similar to this:
$connection = database_connect($parameters) or die("Unable to connect to DB.");
This would theoretically assign the database connection to $connection, or if that failed (and we're assuming here the function would return something that evalues to false in that case), it will end the script with an error message. Because of short-circuiting, if the database connection succeeds, the die() is never evaluated.
Some languages that allow for this construct straight out forbid assignments in conditional/logical statements (like Python) to remove the amiguity the other way round.
PHP went with allowing both, so you just have to learn about your two options once and then code how you'd like, but hopefully you'll be consistent one way or another.
Whenever in doubt, just throw in an extra set of parenthesis, which removes all ambiguity. These will always be the same:
$e = (false || true);
$e = (false or true);
Armed with all that knowledge, I prefer using and or because I feel that it makes the code more readable. I just have a rule not to combine assignments with logical evaluations. But at that point it's just a preference, and consistency matters a lot more here than which side you choose.
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
You should use inner parentheses for precedence ..
The reason is.. the first condition that was wrapped inside the parentheses will be evaluated as a block
As the other user mentioned, && has higher precedence over the || there is no need for the parentheses , but say if your if statement goes on like this..
if($apple==1 || $orange==2 && cake==0)
Then you need to go on with ..
if(($apple==1 || $orange==2) && cake==0)
Sidenote : Always its a good practice to use parantheses...
The expression
$apple==1 && $orange==2 || $cake==0
is equivalent to
($apple==1 && $orange==2) || $cake==0
because && has a higher precedence. As such, it means "when apple is 1 and orange is 2, or cake is 0".
While parenthesis can be used - and can make code more readable - they are not required in this case. However, parenthesis would be required when writing
$apple==1 && ($orange==2 || $cake==0)
which means "when apple is 1 and it is the case that either orange is 2 or cake is 0".