How to use multiple && ORs in a PHP If statement? - php

How I adjust this in order to get the brackets to work?
if (mysql_num_rows($printPricealertsdata) < 5 && ($_POST['pricetype'] == "pricebelow" && $_POST['price'] < $marketdata['priceusd']) OR ($_POST['pricetype'] == "priceabove" && $_POST['price'] > $marketdata['priceusd'])) {
echo "success";
}else{
echo "fail";
}

It would be easier if you told us what you want, and what you get with your code. But since you make me guess, I think you print "success" sometimes, even when the number of rows is 5 or more; and you don't want that. If that's the problem you need brackets around your OR-clause, because && binds closer. Check the manual for the precedence of different operators.
BTW you can use new lines and indentation to make your code more readable:
if ( mysql_num_rows($printPricealertsdata) < 5
&& (
($_POST['pricetype'] == "pricebelow" && $_POST['price'] < $marketdata['priceusd'])
OR
($_POST['pricetype'] == "priceabove" && $_POST['price'] > $marketdata['priceusd'])
)) {
echo "success";
} else {
echo "fail";
}

Try this (without placing brackets around each condition):
if(mysql_num_rows($printPricealertsdata) < 5 && $_POST['pricetype'] == "pricebelow" && $_POST['price'] < $marketdata['priceusd']) OR $_POST['pricetype'] == "priceabove" && $_POST['price'] > $marketdata['priceusd']) {
echo "success";
}else{
echo "fail";
}

Related

Following code is not reaching the else statement

Disclaimer: I'm new to programming, this is a simple exercise I'm doing so go easy on me.
The following code is not reaching (printing) the else statement. Any insight is much appreciated.
$number = '1120011';
// Decimal check
if($number[0] == '+' && $number[1] == '0'){
echo "Error: Can't assign a plus sign on a leading zero!";
// Leading zero check
} elseif($number[0] == '+' || $number[0] == '-'){
echo "Number is decimal";
// Binary check
} elseif (preg_match('~^[01]+$~', $number)) {
echo "Number is binary";
// Code not executing here. For e.g. $number = 1120011
} else {
"Number is non binary";
}
Another question:
Why is the following 'if' not working properly (if I replace it in the code above). I guess it has something to do with bad usage of operators.
if($number[0] == '+' || $number[0] == '-' && $number[1] == '0')
Thanks in advance! :)
For the second question:
if($number[0] == '+' || $number[0] == '-' && $number[1] == '0')
try
if(($number[0] == '+')||(($number[0] == '-')&&($number[1] == '0')))

PHP: Can somebody simplify this IF statement for me?

if($object_type == 'regular') {
if($u_login == $object_user || $u_access >= 3 || $object_access >= 4) {
echo '<p> </p>';
echo '<p><span class="r_button">'.ex_lang('str_btn_delete').'</span></p>';
}
} else
if($object_type == 'comment') {
if($u_login == $object_user || $u_access >= 2 || $object_access >= 4) {
echo '<p> </p>';
echo '<p><span class="r_button">'.ex_lang('str_btn_delete').'</span></p>';
}
}
So the thing that if object is different type user need to have diff access level. How this statement can be simplified for don't having duplicates in it?
I generally forgot about groups in if's, thank you for reminding me it!
if($u_login == $object_user || $object_access >= 4 || ($object_type == 'regular' && $u_access >= 3) || ($object_type == 'comment' && $u_access >= 2)) {
echo '<p> </p>';
echo '<p><span class="r_button">'.ex_lang('str_btn_delete').'</span></p>';
}
Both conditions check for $u_login == $object_user and $object_access >= 4, with only the $object_type and $u_access differing. As such, you can bring these two checks up a level, and check against $object_type and $u_access >= 3 inside the outer condition.
As such, the statement can be re-written like this, shrinking one line of code:
if($u_login == $object_user || $object_access >= 4) {
if($object_type == 'regular' && $u_access >= 3) {
echo '<p> </p>';
echo '<p><span class="r_button">'.ex_lang('str_btn_delete').'</span></p>';
}
else if($object_type == 'comment' && $u_access >= 2) {
echo '<p> </p>';
echo '<p><span class="r_button">'.ex_lang('str_btn_delete').'</span></p>';
}
}
Although depending on your definition of 'simplify', you could also cut out the outer conditional entirely by making use of some brackets:
if(($u_login == $object_user || $object_access >= 4) && ($object_type == 'regular' && $u_access >= 3)) {
echo '<p> </p>';
echo '<p><span class="r_button">'.ex_lang('str_btn_delete').'</span></p>';
}
else if(($u_login == $object_user || $object_access >= 4) && ($object_type == 'comment' && $u_access >= 2)) {
echo '<p> </p>';
echo '<p><span class="r_button">'.ex_lang('str_btn_delete').'</span></p>';
}
However, it's worth nothing that both of your conditionals currently do the exact same thing, so the code could even be simplified to:
echo '<p> </p>';
echo '<p><span class="r_button">'.ex_lang('str_btn_delete').'</span></p>';
Hope this helps! :)
This probably isn't the best place to ask for help refactoring your code, but what the heck. Notice that you have two conditions that are exactly the same and being checked in both if conditions. Why not pull those up to the root level?
if($u_login == $object_user || $object_access >= 4) {
if($object_type == 'regular' && $u_access >= 3) {
echo '<p> </p>';
echo '<p><span class="r_button">'.ex_lang('str_btn_delete').'</span></p>';
}
if($object_type == 'comment' && $u_access >= 2) {
echo '<p> </p>';
echo '<p><span class="r_button">'.ex_lang('str_btn_delete').'</span></p>';
}
}
Notice that we don't need an else anywhere in here because the conditions provided are, by nature, mutually exclusive. This can make readability a bit simpler.

use OR in if codition to print reverse results in php

I am trying echo result "OK" if ( 1 either both ) variable true, i did so far like this
<?php
$user_id = $_SESSION['user_id'];
$point= "select points from users where id = $user_id "; // in db right now points = 2000
$flag= "select m_boost from users where id = $user_id "; // in db right now flag = 1
?>
<div class="box border">
<div class="box-title">
<?php
if($point < 1000 || $flag = 0) {
echo "not ok";
} else { ?>
echo "ok";
<?php }?>
</div>
it's working if i do like this
if(($point == '2000') || ($flag == '0') ){
but i don't want == operator for $point i want less than < $point < '999'
The problem is :
Keep getting result " Not Ok " even one variable (flag = 1) is true in db
Expected Results:
i want to print "OK" if $point > 1000 or flag == 1,
try this
when you using OR condition you should careful about condition and login. you should implement logic in if condition instead of else.
if($point > 1000 || $flag ==1) {
echo "ok";
}else {
echo 'Not ok'
}
or AS YOU WANT
if($point < 1000 ) {
echo "Not ok";
}else if($flag ==0) {
echo 'Not ok'
}esle {
echo 'ok'
}
or you can use this way
$a=false;
if($point < 1000 ) {
$a=true;
}else if($flag ==0) {
$a=true;
}esle {
$a=false;
}
// you can use this variable in your condition.
if($a) {
echo "ok";
}else {
echo 'Not ok'
}
When you are using OR, if the first condition is met the second is disregarded.
Also, make sure you use double equals (==) for comparison, not single equals (=) which means assignment.
Therefore you want to replace this:
if($point < 1000 || $flag = 0) {
With one of these:
SWAPPED AROUND
if($flag == 0 || $point < 1000) {
or
USING && INSTEAD
if($point < 1000 && $flag == 0) {
Depending on what behaviour you're looking for. It's a little unclear - so any additional clarification from you would be helpful.
Once you get something that you think is working, try to test all possible combinations so that you can be confident it works how you wish.
$flag = 0
You're setting the variable's value to 0.
Example:
$flag = 1;
if($a < $b || $flag = 0){ //$flag's value is 0 now.
...
...
}
In conditions comparisons, the right operator is "==".
if($point < 1000 || $flag == 0) {
List of comparison operators
but i don't want == operator for $point i want less than < $point <
'999'
Didn't fully understood your goals, but maybe:
For checking a value in a range the right logical operator should be "and" (&&);
if($point > 0 && $point < 999){
List of logical operators
Update:
if $point > 1000 or flag == 1
if($point > 1000 || $flag == 1){
echo "ok"
}
if (!$flag && $point < 1000)
{
echo "Not OK";
} else {
echo "OK";
}
Writing this into a truth-table:
flag point result
0 < 1000 Not OK
1 < 1000 OK
0 >=1000 OK
1 >=1000 OK
Worked for me now as per my question
i think we are all here for some contribution reason, flagging down a question is not a way, if you got solution than respond otherwise my question was 100% clear.

if and else statement mixed up

I am doing the following if else statement below but number (//1) and number (//4) get executed at the same time, I am finding it abit hard to understand why.
<?php
//1
if($a == 1 && count($b) == 0) {
// do this
}
//2
elseif ($a == 1 && count($b) > 0) {
// do that
}
//3
if($a== 0 && count($b) == 0) {
// do a different thing
}
//4
else {
// do the last thing
}
?>
I have done this and it works but i think the should be a more suitable way for not using elseif for this.
else if($a== 0 && count($b) > 0) {
// do the last thing
}
but number (//1) and number (//4) get executed at the same time
It's because you don't have else before the if on //3
//3
if($a== 0 &&
Change to elseif($a== 0 &&
At the moment you have two separate IF conditions
You're missing a closing brace after your first if.
Also, you have a weird operator inside your first condition : $$. Maybe you intended to type &&?
$a = 10;
if ($a == 5) {
echo 'ok';
} elseif ($a == 10) { // $a is equal to 10, so it executes;
echo 'not_ok';
}
if ($a > 20) {
echo 'ok_ok';
} else { // $a is not >20 so else statement executes
echo 'not_not';
}
final result: not_oknot_not
If you are performing such tests on one and the same assignee, but different values, you might not want to execute more than one?
I guess you need elseif where third block is if
if ($a == 5) {
echo 'ok';
} elseif ($a == 10) { // $a is equal to 10, so it executes and stops the block;
echo 'not_ok';
} elseif ($a > 20) {
echo 'ok_ok';
} else { // $a is not >20, but the block was stopped on first elseif
echo 'not_not';
}
produces not_ok
Even if you move the else statement after the first elseif block as was suggested
if ($a > 20) {
echo 'ok_ok';
}
will execute, and if it's true, it will produce result, which again will result in double result
You might want to do this...
if ($a == 1 && count($b) == 0) {
// do this
}
//2
elseif ($a == 1 && count($b) > 0) {
// do that
}
elseif ($a== 0 && count($b) == 0) {
// do a different thing
}
else {
// do the last thing
}
The reason they get executed at the same time is that... Well technically they're not executed at the same time since it's procedural, but they both get executed because they are both different if else conditions. If you want only 1 execution, you should combine them :)

give error if number is anything other than 1 or 0

<?php
$gender = 0;
if (($gender != 0) || ($gender != 1))
{
die('error:Must select a gender.');
}
?>
This should give a error if the gender is anything other than 1 or 0. So if i gave 5 it should die. If i gave it 1 it should not die. If i give it 0 it should not die.
I was thinking about a few work arounds
<?php
$gender = 0;
if ($gender == 0)
{
//number is okay
}
else if ($gender == 1)
{
//number is okay
}
else
{
die('error:Must select a gender.');
}
?>
Well that looks sloppy and it would work or i could create a array with 0 and 1 and check if its in it or not. Kinda overkill i think.
Not sure what i'm doing wrong.
You're using the wrong boolean operator:
if (($gender != 0) || ($gender != 1)) {
}
This will be entered if gender is 0, too, because it isn't 1, and vice versa. What you need to do is:
if (($gender != 0) && ($gender != 1)) {
}
Look at this table:
gender A (gender != 0) B (gender != 1) A || B A && B
----------------------------------------------------------------
0 false true true false
1 true false true false
5 true true true true
Also note Joshua's suggestion in the comments:
if (($gender == 0) || ($gender == 1)) {
/* number is ok */
} else {
die();
}
which is a bit longer, but more readable.
if (($gender != 0) && ($gender != 1))
^^
You want to die if both tests are true.
Change the or (||) to and (&&) and it should work, as you only want to fail, when it’s both not 0 and not 1.

Categories