Using AND/OR in if else PHP statement

后端 未结 10 1914
梦如初夏
梦如初夏 2020-12-12 19:16

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 
         


        
相关标签:
10条回答
  • 2020-12-12 20:16

    A bit late but don't matter...
    the question is "How do you use...?" short answer is you are doing it correct


    The other question would be "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 1, now what if

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

    This will return "TRUE" even if B still not the value we asking for, there is another way to return TRUE without the use of OR / || and that would be XOR

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

    in this case we need only one of our variables to be true BUT NOT BOTH if both are TRUE the result would be FALSE.

    I hope this helps...

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

    0 讨论(0)
  • 2020-12-12 20:17

    You have 2 issues here.

    1. use == for comparison. You've used = which is for assignment.

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

    0 讨论(0)
  • 2020-12-12 20:21

    AND is && and OR is || like in C.

    0 讨论(0)
  • 2020-12-12 20:22

    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.
    0 讨论(0)
提交回复
热议问题