if a value is set make a check otherwise ignore - php

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']

Related

Is there a way to state multiple conditions associated with another condition as a whole?

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

Multi Conditional PHP IF statement only if variable has value

I have the following if statement, I want to return TRUE if example = sidebar, date start < now, and date end > now. It works as is but the problem is if one of the values doesn't exist, the statement is false.
How can I change the statement to only add conditions if each variable ($position,$date_start, $date_end) isset ? If one of the variables is not set, that portion the condition will be ignored.
For example, if there was no date_end, it would only evaluate for position and date_start
<?php
if ($position == "sidebar" &&
$date_start < strtotime('now') &&
$date_end > strtotime('now')):
?>
You want to check if each variable is not set OR if it equals the desired value
<?php if (
(!isset($position) || $position == "sidebar") &&
(!isset($date_start) || $date_start < strtotime('now')) &&
(!isset($date_end) || $date_end > strtotime('now'))
): ?>
If you want to allow unset, null, '' (empty string), and 0 values in addition to the specified string values, then you can check empty() instead of isset()
<?php if (
(!empty($position) || $position == "sidebar") &&
(!empty($date_start) || $date_start < strtotime('now')) &&
(!empty($date_end) || $date_end > strtotime('now'))
): ?>

PHP Operator failing

I am using this code to select and compare dates and information from 2 separate MySQLi tables. The code should produce the "discount_amount"IF the discount code entered is the same as the discount code in the database OR the discount code in the database is set to "open". Yet even with the dates set and the code set to "open" I am only getting the discount_amount if I type "open" in to the "$design_discount_code" manually.
if($discount_code == $design_discount_code or $discount_code == 'open' && $date >= $discount_start_date && $date <= $discount_end_date){
$design_price_total = $discount_amount;
$discount = 'yes';
} else {
$design_price_total = $original_price;
$discount = 'no';
}
I have also attempted to change the PHP operator to || and xor with no better results. Any ideas as to why it is only returning 'yes' when I type "open" and not automatically as it should?
You seem to assume that or has higher precedence than and. This is incorrect. You should use parentheses to indicate that the or check is one of the and conditions. Like this:
if(($discount_code == $design_discount_code or $discount_code == 'open') and $date >= $discount_start_date and $date <= $discount_end_date)
Also note that mixing the English and/or with the symbolic &&/|| is not a great idea.
Check the priority of the and and or, maybe you can use parentheses () to specify which should be evaluated first.
if( ( $discount_code == $design_discount_code || $discount_code == 'open') && ( $date >= $discount_start_date && $date <= $discount_end_date) )

Getting wrong plural from calculation

$n=21;
$p=$n%10==1 && $n%100!=11 ? 0 : $n%10>=2 && $n%10<=4 && ($n%100<10 || $n%100>=20) ? 1 : 2;
why is $p = 2?
it is supposed to be $p = 0!
is it a bug or am I missing something?
I got this from trying to get the plural form for Russian on: http://www.gnu.org/s/hello/manual/gettext/Plural-forms.html
should be this one:
$p=($n%10==1 && $n%100!=11) ? 0 : (($n%10>=2 && $n%10<=4 && ($n%100<10 || $n%100>=20)) ? 1 : 2);
the error was in missing bracets
you can see here: http://php.net/manual/en/language.operators.comparison.php that "It is recommended that you avoid "stacking" ternary expressions. PHP's behaviour when using more than one ternary operator within a single statement is non-obvious". You can see that if you enclose the else part for the first if between ( and ) you will get another result:
$p=$n%10==1 && $n%100!=11 ? 0 : ($n%10>=2 && $n%10<=4 && ($n%100<10 || $n%100>=20) ? 1 : 2);
Maybe you should consider changing you statement to a "regular" if block, something like:
if ($n%10==1 && $n%100!=11)
{
$p =0 ;
}
elseif ($n%10>=2 && $n%10<=4 && ($n%100<10 || $n%100>=20))
{
$p = 1;
}
else
{
$p= 2;
}
this way being easier to read

How to get OR(||) and AND (&&) statements to work together? PHP

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))

Categories