I can't get this code to work
if ($title != 'Main' || $nSpace == 56 || $nSpace == 30 && $ifHome != 361)
So i settled for this code
if ($title != 'ArticleCategories' || $nSpace == 1)
if ($nSpace == 0 && $ifHome != 1)
So, i am now wondering how can i get those two lines into one line in the way so that it works? I know how to get multiple or || statements into one line but not the or || and and && statements together.
Use parens to group the logic together in a manner that makes sense for your program.
if ($title != 'Main' || $nSpace == 56 || ($nSpace == 30 && $ifHome != 361))
Related
I am trying to consolidate a few different if statements. What I am trying to accomplish would read something like this:
If (this is true and this is true) OR (this is true and this is true) AND (This is true)
So, one at least one of the first two sets of parentheses would need to be true, and if one of those is true, then also the last set of parentheses would need to be true, in order for the code inside to be executed.
Here is the specific code I am (unsuccessfully) trying to make work:
if(($calc->number % 2 == 1 && $calc->doubleColor == 'b2' | $calc->number % 2 == 0 && $calc->doubleColor = 'r2') && in_array($calc->number, $backToBet)){
}
Is there a way to do this? A possibility? Is there any drawback to getting a lot into a single if statement?
EDIT
$blackMatch = $calc->number % 2 == 1 && $calc->doubleColor == 'b2';
$redMatch = $calc->number % 2 == 0 && $calc->doubleColor = 'r2';
$numberMatch = in_array($calc->number, $backToBet);
if(($blackMatch || $redMatch) && $numberMatch){
}
/ ** Calc->number = 2, $blackMatch = false, $redMatch = false,
$numberMatch array contains 2 **/
Basically what I end with is a 'true' result, even though neither of the conditions within the inner parentheses are satisfied.
to make code easier to read, I'd suggest to use separate variables, like this:
$condition1 = ($calc->number % 2 == 1) && ($calc->doubleColor == 'b2');
$condition2 = ($calc->number % 2 == 0) && ($calc->doubleColor == 'r2');
$condition3 = in_array($calc->number, $backToBet);
if (($condition1 || $condition2) && $condition3) {
}
two things to note:
|| is logical OR, | is bitwise OR
== is comparison, = is assignment
I am just making this short but if there are lots of conditions in this if statement, is there a way to shorten it?
if(($post->id <= 37) || ($post->id == 42) || ($post->id == 44) || ($post->id == 45) || ($post->id == 46))){
//code here
}
it'll all be id but the number would be different and there's no ranging or old / even numbers just randoms
Is there a way to make it shorter?
if($post->id <= 37 || in_array( $post->id, array( 42,44,45,46)))
I've been scratching my head for a while trying to figure this out.
I have an array which look something like...
$param = array(
"conditionType" => $_GET['conditionType'] ? strtolower($_GET['conditionType']) : "all",
"minPrice" => $_GET['minPrice'] ? $_GET['minPrice'] : false,
"maxPrice" => $_GET['maxPrice'] ? $_GET['maxPrice'] : false,
);
I need to check to make sure that all books returned are >= the min price and <= the max price, but if these user chooses not to specify a min or max price than the if statement needs to ignore this check.
if(($param['conditionType'] == "all"
|| ($param['conditionType'] == "new" && strtolower($book['sub_condition']) == "new")
|| ($param['conditionType'] == "used" && strtolower($book['sub_condition']) != "new"))
&& ($param['minPrice'] && $book['price'] >= $param['minPrice'])
&& ($param['maxPrice'] && $book['price'] <= $param['maxPrice'])
&& $book['price'] != "null"
){
$books[] = array(
"link" => $book['link'],
"listing_condition" => $book['sub_condition'],
"listing_price" => $book['price']
);
}
What can I change in the if statement to make sure this happens?
You need to restructure the parts that read like
&& ($param['minPrice'] && $book['price'] >= $param['minPrice'])
to instead read like
&& (!$param['minPrice'] || $book['price'] >= $param['minPrice'])
This follows your stated logic: either a min price should not be set, or it must be less than or equal to the book price.
you just need to negate this $param['minPrice'] to !$param['minPrice']
if I have something like:
if (($browser->getBrowser() == Browser::BROWSER_SAFARI && $browser->getVersion() >= 3 ) ||
($browser->getBrowser() == Browser::BROWSER_CHROME && $browser->getVersion() >= 5 ))
{
// code here
}
but I really want to say also if Chrome >= 5 but less 6...
I will add an else if for 6+
later on in else () less than version 5 would fall into..
How would I write >= 5 but < 6?
So you can do this directly by adding another condition:
if (($browser->getBrowser() == Browser::BROWSER_SAFARI
&& $browser->getVersion() >= 3 ) ||
($browser->getBrowser() == Browser::BROWSER_CHROME
&& $browser->getVersion() >= 5 && $browser->getVersion() < 6 ))
{
// code here
}
I'm assuming getVersion must be capable of returning non-integer numbers, otherwise you could just check for equality ($browser->getVersion() == 5).
In the PHP below if I compare a variable using == it works as I would expect it to, if I use != then my code breaks, can someone explain or help?
$_GET['p'] = 'home';
// DOES NOT work, it will always return "show JS" regardless to what string I have
if ($_GET['p'] != 'home' || $_GET['p'] != 'create.account'){
echo 'show JS';
}else{
echo 'do not show JS';
}
// Works as I would expect it to
if ($_GET['p'] == 'home' || $_GET['p'] == 'create.account'){
echo 'do not show JS';
}else{
echo 'show JS';
}
$_GET['p'] can't be two different things at the same time. You say int the first expression;
p not home or not create.account. Its always true. You should use && instead of ||
You can see the problem by DeMorgans Laws for negation
!( A || B ) === (!A && !B)
The solution you give is impossible because there is no way for both statements to be false, because that would imply the string is equivalent to both the compared strings. The only way the else block would be hit is if both were false (because of the OR statement).
(X != A) || (X != B)
≡ !(X == A) || !(X == B)
≡ !((X == A) && (X == B))
(X == A) && (X == B) is always false since the condition A != B but X cannot be both A and B at the same time. So !((X == A) && (X == B)) and your (X != A) || (X != B) is always true.
if ($_GET['p'] != 'home' && $_GET['p'] != 'create.account')