Complex php if statement - php

I'm trying to generate all different combinations for something that have x>=y, x<=y
ex: $abc <= 123 && $abc >= 123 && $abb >= 123 && $abb <= 123 && $acc >= 123
however, I don't want things such as "$abc <= 123 && $abc >= 123" to be saved (I'd want the combination: $abc <= 123 && $abb >= 123 && $acc >= 123 to be saved, no conflicting arguments), so I'm trying to use an if statement to filter them out, sometimes there are multiple conflicting arguments such as "$abc <= 123 && $abc >= 123 && $abb >= 123 && $abb <= 123" but I'm having trouble
$string = '$abc <= 123 && $abc >= 123 && $abb >= 123 && $abb <= 123 && $acc >= 123';
if(substr_count($string, "abc <=") == 0 && substr_count($string, "abc >=") == 1 || substr_count($string, "abc <=") == 1 && substr_count($string, "abc >=") == 0) {
echo("hello");
}
In this statement it won't return hello, which is right, but once you remove one conflicting argument from $string it will return hello, but I don't want this because still another conflicting argument exists. Is it possible to do something like this or I must do something like:
substr_count($string, "abc <=") == 0 && substr_count($string, "abc >=") == 1 && substr_count($string, "abb <=") == 1 && substr_count($string, "abb >=") == 0
Please advise
Sorry if this is a poorly asked question, it's hard to explain

This is what bitmasks are for.

Related

Remove values from array depending on conditions

This is more of an algorithm problem than a PHP problem, but I can't seem to figure it out!
I've got a multi dimensional array as following :
[
210875 => ["2", "1"],
129096 => ["2", "2"]
]
There are 2 ids but there could be more. I want to keep in my table only 1 id depending on these conditions.
Pseudo code:
if a[0] == 1 and a[1] >= 1: # the id gets deleted right away without looking at other conditions
if a[0] >= 2 and a[1] == 2: # it stays if nothing below fits else it gets deleted
if a[0] >= 3 and a[1] == 3: # it stays if nothing below fits else it gets deleted
if a[0] >= 8 and a[1] == 4: # it stays if nothing below fits else it gets deleted
# ... etc
If two fit the same conditions then one of them randomly gets deleted.
For example (pseudo code):
a[0] == 2 and a[1] == 1
a[0] == 2 and a[1] == 2
I've tried doing the following, but I can't figure out how to dynamically have if-conditions made dependent on my algorithm :
$sub_array = array();
foreach($array as $k=>$v) {
if($v[0] == 1 && $v[1] == 1) continue;
if($v[0] >= 2 && $v[1] == 2) $sub_array[$k] = $v;
if($v[0] >= 6 && $v[1] == 3) $sub_array[$k] = $v;
if($v[0] >= 8 && $v[1] == 4) $sub_array[$k] = $v;
// etc ...
}
Also, when I do that, how to know which one is the highest in my sub array?
It is not entirely clear to me what your conditions are, but to me it looks like you really are giving a rank to each of your rows, where in the end you want to keep the row with the highest rank.
Even if my interpretation of your rules might not be exactly what you intended, you can tune it by adapting the scores in the code below. I would also suggest to swap the conditions so that the == comparison comes first. That will be a bit more efficient:
$keepKey = null;
$bestScore = -1;
foreach($array as $k=>$v) {
$score = 0;
if ($v[1] == 1 && $v[0] == 1) $score = 1;
if ($v[1] == 2 && $v[0] >= 2) $score = 2;
if ($v[1] == 3 && $v[0] >= 6) $score = 3;
if ($v[1] == 4 && $v[0] >= 8) $score = 4;
// ... etc ...
if ($score > $bestScore) {
$keepKey = $k;
$bestScore = $score;
}
}
$sub_array = [$keepKey => $array[$keepKey]]; // Only keep the entry with highest score
If it is important to really pick a random one in case of ties, then shuffle the keys before starting the loop:
$keepKey = null;
$bestScore = -1;
$keys = array_keys($array); // <---
shuffle($keys); // <
foreach($keys as $k) { // <
$v = $array[$k]; // <---
$score = 0;
if ($v[1] == 1 && $v[0] == 1) $score = 1;
if ($v[1] == 2 && $v[0] >= 2) $score = 2;
if ($v[1] == 3 && $v[0] >= 6) $score = 3;
if ($v[1] == 4 && $v[0] >= 8) $score = 4;
echo $score . "\n";
if ($score > $bestScore) {
$keepKey = $k;
$bestScore = $score;
}
}
$sub_array = [$keepKey => $array[$keepKey]];

Multiple logical operators in if..else staterment in php

firstname and lastname are being submitted from a HTML form.
In PHP i want to check that both of them must be atleast 3 characters long and maximum upto 18 characters.
How can i check it in an if else statement
Example code is below
$f = $_POST['firstname'];
$l = $_POST['lastname'];
if((strlen($f) < 3 || strlen($f) > 16) && (strlen($l) < 3 || strlen($l) > 16))
{
echo "Firstname and lastname must be between 3 and 16 characters";
exit();
}
It is not working what i have done wrong.....
Thanks for help and I hope you understand my problem
if((strlen($f) < 3 || strlen($f) > 16) || (strlen($l) < 3 || strlen($l) > 16))
Replace && in the middle by ||
What you did only throws the error when both aren't valid.
You want to throw it when one of them is.
try this..
if((strlen($f) <= 3 && strlen($f) >= 18 ) && (strlen($l) <= 3 && strlen($l) >= 18 ))
{
echo "Firstname and lastname must be between 3 and 16 characters";
}
else
{
//query here..
}

IF condition using combination of AND and OR [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 8 years ago.
Improve this question
I'd like to know if my conditions are written with the right way (for the first statement). (in therm of optimization, readability)
if(($nb_post_by_user >= 3 && $nb_post_by_user < 5) || ( $nb_post_by_user >= 3 && ($nb_comm_by_user < 15 || $percent_voted < 25) )){
// Call function A();
}
else if( ($nb_post_by_user >= 5 && $nb_post_by_user < 10) && ($nb_comm_by_user >= 15 && $nb_comm_by_user < 30) && ($percent_voted >= 25 && $percent_voted < 70) ){
// Call function B();
}
Or does the first statement could be written that way? (second statement will be then nested).
if($nb_post_by_user >= 3){
if($nb_comm_by_user >= 15 || $percent_voted >= 25){
// Call function B
}
else{
// Call function A
}
}
if(($nb_post_by_user >= 3 && $nb_post_by_user < 5) || ( $nb_post_by_user >= 3 && ($nb_comm_by_user < 15 || $percent_voted < 25) )){
// Call function A();
} else if( ($nb_post_by_user >= 5 && $nb_post_by_user < 10) && ($nb_comm_by_user >= 15 && $nb_comm_by_user < 30) && ($percent_voted >= 25 && $percent_voted < 70) ){
// Call function B();
}
Edited one :
if($nb_post_by_user >= 3) {
if($nb_post_by_user < 5 || $nb_comm_by_user < 15 || $percent_voted < 25) {
// Call function A();
} else if($nb_post_by_user < 10 && $nb_comm_by_user >= 15 && $nb_comm_by_user < 30 && $percent_voted >= 25 && $percent_voted < 70) {
// Call function B();
}
}
In my opinion both the statements are not doing the same piece of work. The best logical reason i came up with is this :
The first piece of code will get executed if one of the condition written in the if statements are true. But in the second piece of code. The code will always gets executed if the first if condition holds true because of the presence of else statement instead of if else. So both are doing a different work.
hope it helps :)

How to make a correct if-statement to filter out values from a xml-file

Edit 3:
As requested, I'm trying to simplify my question.
Here is a sample of some of my data from a xml file:
<entry>
<title>Entry 1</title>
<f:max_value_a>499 999</f:max_value_a>
<f:max_value_b>999 999</f:max_value_b>
<f:min_value_a>0</f:min_value_a>
<f:min_value_b>500 000</f:min_value_b>
<f:min_value_c>1 000 000</f:min_value_c>
<f:value_for_a>5,10</f:value_for_a>
<f:value_for_b>4,50</f:value_for_b>
<f:value_for_c>3,90</f:value_for_c>
</entry>
<entry>
<title>Entry 2</title>
<f:min_value_a>0</f:min_value_a>
<f:value_for_a>4,20</f:value_for_a>
</entry>
<entry>
<title>Entry 3</title>
<f:max_value_a>1 999 999</f:max_value_a>
<f:min_value_a>100 000</f:min_value_a>
<f:min_value_b>2 000 000</f:min_value_b>
<f:value_for_a>3,735</f:value_for_a>
<f:value_for_b>3,445</f:value_for_b>
</entry>
f:value_for_d is the highest value, and f:value_for_c is lower than d, and so on.
I have a dynamic targetvalue (lets just go with 2 000 000 in this example)
I want to get the value where max_value is greater than the targetvalue, but sometimes max_value is not defined and then set to "0". "0" in max_value should mean unlimited "roof". The min_value can not be greater than targetvalue, but sometimes min_value is not defined and then set to "0". "0" min_value should mean a unlimited "floor".
I have tried with this code
if ($value_for_d > 0 ){
if (($min_value_d <= $targetvalue) xor ($min_value_d == 0)){
if (($max_value_d >= $targetvalue) xor ($max_value_d == 0)){
$query_result = TRUE;
$value = $value_for_d;
}
}
}elseif ($value_for_c > 0 ){
if (($min_value_c <= $targetvalue) xor ($min_value_c == 0)){
if (($max_value_c >= $targetvalue) xor ($max_value_c == 0)){
$query_result = TRUE;
$value = $value_for_c;
}
}
}elseif ($value_for_b > 0 ){
if (($min_value_b <= $targetvalue) xor ($min_value_b == 0)){
if (($max_value_b >= $targetvalue) xor ($max_value_b == 0)){
$query_result = TRUE;
$value = $value_for_b;
}
}
}elseif ($value_for_a > 0 ){
if (($min_value_a <= $targetvalue) xor ($min_value_a == 0)){
if (($max_value_a >= $targetvalue) xor ($max_value_a == 0)){
$query_result = TRUE;
$value = $value_for_a;
}
}
}
If I run this code with a targetvalue of "2 000 000", I get this result:
Entry 1 - 3.9 (correct value is 3.9)
Entry 2 - 0 (correct value is 4.2)
Entry 3 - 3.445 (correct value is 3.445)
If I set the targetvalue to even lower, to 500 000, I get 0 on all my entries.
Edit 4:
If I do:
var_dump($min_value_d,$max_value_d,$min_value_c,$max_value_c,$min_value_b,$max_‌​value_b,$min_value_a,$max_value_a);
I get this output: https://dpaste.de/DtjhO/
If the regions are always exclusive and always in the right order, I'd suggest you do something like this:
<?php
if($min_value_d <= $targetvalue) {
$value = $value_for_d;
} else if($min_value_c <= $targetvalue) {
$value = $value_for_c;
} else if($min_value_b <= $targetvalue) {
$value = $value_for_b;
} else if($min_value_b <= $targetvalue) {
$value = $value_for_b;
}
?>
However, if they are not always exclusive, you should be returning some form of array, as it may fulfil multiple criteria:
Assuming that $min_value_x is not set when it is not in the XML (As in, no minimum) and $max_value_x is not set when it is not in the XML (As in, no maximum)
<?php
$values = array();
//if (there is no minimum or its smaller than target) and (there is no maximum or its larger than target) and at least one of them is non-null
if((is_null($min_value_d) || preg_replace('/([^0-9.])/i', '',$min_value_d) <= $targetvalue) && (is_null($max_value_d) || preg_replace('/([^0-9.])/i', '',$max_value_d) >= $targetvalue) && !(is_null($min_value_d) && is_null($max_value_d))) $values[] = $value_for_d;
if((is_null($min_value_c) || preg_replace('/([^0-9.])/i', '',$min_value_c) <= $targetvalue) && (is_null($max_value_c) || preg_replace('/([^0-9.])/i', '',$max_value_c) >= $targetvalue) && !(is_null($min_value_c) && is_null($max_value_c))) $values[] = $value_for_c;
if((is_null($min_value_b) || preg_replace('/([^0-9.])/i', '',$min_value_b) <= $targetvalue) && (is_null($max_value_b) || preg_replace('/([^0-9.])/i', '',$max_value_b) >= $targetvalue) && !(is_null($min_value_b) && is_null($max_value_b))) $values[] = $value_for_b;
if((is_null($min_value_a) || preg_replace('/([^0-9.])/i', '',$min_value_a) <= $targetvalue) && (is_null($max_value_a) || preg_replace('/([^0-9.])/i', '',$max_value_a) >= $targetvalue) && !(is_null($min_value_a) && is_null($max_value_a))) $values[] = $value_for_a;
?>
As it turns out, your items are formatted with spaces, thus when converted to a float for interpretation it only looked at the first set of numbers. preg_replace('/([^0-9.])/i', '', $var) removes anything non-numerical. (This is quite computationally expensive, mind)

PHP "AND or &&" Operator Problem

I have:
if($this->itemCount <= 11 ) {
$item['subtotal'] = 12.95 * $item['qty'];
$item['price'] = 12.95;
$this->update_item($item['id'], $item['qty'], $item['price']);
}
But I need an "and" operator in there to check whether or not its also equal to or greater than 2.
if($this->itemCount <= 11 && => 2 )
I don't know how to do this in PHP. :(
if($this->itemCount <= 11 && $this->itemCount >= 2) {
// Your code here
}
if($this->itemCount <= 11 && $this->itemCount => 2 )
if($this->itemCount <= 11 && $this->itemCount >= 2 )
You just need to write the full expression out:
if($this->itemCount <= 11 && $this->itemCount >= 2 )

Categories