Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using AND/OR in if else PHP statement

How do you use 'AND/OR' in an if else PHP statement? Would it be:

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"); }  
like image 544
methuselah Avatar asked Dec 10 '10 04:12

methuselah


People also ask

Can you use && in an if statement?

In the logical AND ( && ) operator, if both conditions are true , then the if block will be executed. If one or both of the conditions are false , then the else block will be executed.

How many && and/or || operators can be used in an if statement?

There is no limit to the number of && you use in a statement. So it will work with 4, 5, 100. It fails because some of the conditions are falsey.

Can you use an AND operator in an if statement?

When you combine each one of them with an IF statement, they read like this: AND – =IF(AND(Something is True, Something else is True), Value if True, Value if False) OR – =IF(OR(Something is True, Something else is True), Value if True, Value if False) NOT – =IF(NOT(Something is True), Value if True, Value if False)


1 Answers

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


Two things though:

  • Many programmers prefer && and || instead of and and or, but they work the same (safe for precedence).
  • $status = 'clear' should probably be $status == 'clear'. = is assignment, == is comparison.
like image 84
deceze Avatar answered Oct 13 '22 06:10

deceze