PHP Conditionals --> If Else with multiple decisions - php

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

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

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 comparing 2 co-ords

I don't know why, but this doesn't seem to work.
Basically I want this to be true if the player1 ($playerX, $playerY) is within one square from player2 ($rs[x], $rs[y])
if (($rs[x] > $playerX-2 or $rs[x] < $playerX+2) && ($rs[y] > $playerY-2 or $rs[y] < $playerY+2)) {
// code
Any idea what I'm doing wrong?
If you replace with some test values, you might get:
if( (3 > 5 or 3 < 9) && (13 > 15 or 13 < 19))
Clearly, this is always true.
You should use && instead of or in this case.
It's just a case of replacing or with and. Here's a neater way of writing your if statement for bonus points:
if(abs($playerX - $rs[x]) < 2 && abs($playerY - $rs[y]) < 2) {
// code
Your conditions end up on true due to the OR operator
if (($rs[x] > $playerX-2 && $rs[x] < $playerX+2) && ($rs[y] > $playerY-2 && $rs[y] < $playerY+2)) {
//your code
}

Categories