Using AND / OR in if else PHP expression

How do you use 'AND / OR' in an if else else statement? This will:

1) And

if ($status = 'clear' AND $pRent == 0) { mysql_query("UPDATE rent SET dNo = '$id', status = 'clear', colour = '#3C0' WHERE rent.id = $id"); } 

2) OR

 if ($status = 'clear' OR $pRent == 0) { mysql_query("UPDATE rent SET dNo = '$id', status = 'clear', colour = '#3C0' WHERE rent.id = $id"); } 
+47
php if-statement
Dec 10 '10 at 4:55
source share
10 answers

Yes. The answer is yes. http://www.php.net/manual/en/language.operators.logical.php




Two things:

  • Many programmers prefer && and || instead of and and or , but they work the same (safe for priority).
  • $status = 'clear' must be $status == 'clear' . = is an assignment, == is a mapping.
+77
Dec 10 '10 at 4:59
source share

A little late, but not important ...
question: "How do you use ...?" short answer - you do it right




Another question: "When do you use it?"
I use && instead of AND and || instead of OR .
 $a = 1 $b = 3 

Now,

 if ($a == 1 && $b == 1) { TRUE } else { FALSE } 

in this case, the result is "FALSE" because B is not equal to 1, now what if

 if ($a == 1 || $b == 1) { TRUE } else { FALSE } 

This will return TRUE, even if B is still not the value we are requesting, there is another way to return TRUE without using OR / || and it will be XOR

 if ($a == 1 xor $b == 1) { TRUE } else { FALSE } 

in this case, we need only one of our variables, BUT NOT BOTH, if both of them are TRUE, the result will be FALSE.

Hope this helps ...

more in:
http://www.php.net/manual/en/language.operators.logical.php

+8
Feb 06 '14 at 22:31
source share

You have 2 questions.

  • use == to compare. You used = , which is for the assignment.

  • use && for "and" and || for "or." and and or will work, but they are unconventional.

+7
Dec 10 '10 at 4:59
source share

There are some jokes and misleading comments, even partially incorrect information in the answers here. I would like to try to improve them:

First , as some have pointed out, you have a bug in the code related to the question:

 if ($status = 'clear' AND $pRent == 0) 

should be (pay attention to == instead of = in the first part):

 if ($status == 'clear' AND $pRent == 0) 

which in this case is functionally equivalent

 if ($status == 'clear' && $pRent == 0) 

Second , note that these operators ( and or && || ) are short-circuit operators. This means that if the answer can be determined with certainty from the first expression, the second will never be evaluated. Again, this does not matter for your debugged line above, but it is extremely important when you combine these operators with the destination, because

Third , the real difference between and or and && || is their operator priority . In particular, it is important that && || had higher priority than the assignment operators ( = += -= *= **= /= .= %= &= |= ^= <<= >>= ), and and or had lower precision than the assignment operators . Thus, in a statement that combines the use of assignment and logical evaluation, it is important which one to choose.

Modified examples from the PHP page for logical operators :

 $e = false || true; 

will evaluate to true and assign this value $e , because || has a higher operator precedence than = , and therefore it essentially evaluates the following:

 $e = (false || true); 

However

 $e = false or true; 

assigns false to $e (and then performs the or operation and evaluates to true ), since = has a higher operator priority than or , essentially evaluating as follows:

 ($e = false) or true; 

The fact that this ambiguity even exists allows many programmers to always use && || and then everything works clearly, as one would expect in a language such as C, i.e. logical operations first, then assignment.

Some languages, such as Perl, use this construct often in a format like this:

 $connection = database_connect($parameters) or die("Unable to connect to DB."); 

This would theoretically assign a connection to the database $connection , or if it didn’t work (and we assume that the function returns a value here, which in this case will be false ), it will end the script with an error message. Due to a short circuit, if the database connection succeeds, die() never evaluated.

Some languages ​​that allow this construction explicitly prohibit assignments in conditional / logical operations (e.g. Python) to eliminate bias in the opposite direction.

PHP went by resolving both, so you just need to find out about your two options once, and then the code as you want, but hopefully you will be consistent anyway.

Whenever in doubt, just add an extra set of brackets that removes all the ambiguity. They will always be the same:

 $e = (false || true); $e = (false or true); 

Armed with all this knowledge, I prefer to use and or , because I feel that it makes the code more readable. I just have a rule not to combine assignments with logical ratings. But at this moment it is just a preference, and the sequence here is much more important than which one you choose.

+7
Apr 07 '16 at 23:14
source share

And there is && , and OR - || as in C.

+5
Dec 10 '10 at 4:57
source share

AND and OR are just syntactic sugar for && and || , for example, in JavaScript or other C syntax languages

This appears AND and OR has a lower priority than their C style equivalents.

+4
Dec 10 '10 at 4:59
source share

for AND you use

 if ($status = 'clear' && $pRent == 0) { mysql_query("UPDATE rent SET dNo = '$id', status = 'clear', colour = '#3C0' WHERE rent.id = $id"); } 

for OR you use

 if ($status = 'clear' || $pRent == 0) { mysql_query("UPDATE rent SET dNo = '$id', status = 'clear', colour = '#3C0' WHERE rent.id = $id"); } 
+1
Dec 10 '10 at 5:00
source share

I think I have a little confusion here. :) But it seems that no one else has it.

Are you asking which one should be used in this scenario? If yes, then And this is the correct answer .

If you ask how operators work, then

In php, both AND, & , and OR, || . If you are new to programming and php is one of your first languages, I suggest using AND and OR because it improves readability and reduces confusion when checking. But if you are already familiar with any other languages, then you may already be familiar with && and || operators.

0
Dec 10 '10 at 6:26
source share
 <?php $val1 = rand(1,4); $val2=rand(1,4); if ($pars[$last0] == "reviews" && $pars[$last] > 0) { echo widget("Bootstrap Theme - Member Profile - Read Review",'',$w[website_id],$w); } else { ?> <div class="w100"> <div style="background:transparent!important;" class="w100 well" id="postreview"> <?php $_GET[user_id] = $user[user_id]; $_GET[member_id] = $_COOKIE[userid]; $_GET[subaction] = "savereview"; $_GET[ip] = $_SERVER[REMOTE_ADDR]; $_GET[httpr] = $_ENV[requesturl]; echo form("member_review","",$w[website_id],$w);?> </div></div> 

ive replaced 'else' with '& &' so both are located ... argh

0
Oct 19 '16 at 4:20
source share

And does not work in my PHP code.

Perhaps the server version?

"& &" works great.

-one
Feb 20 '14 at 19:13
source share



All Articles