使用和/或在else PHP语句中。

时间:2022-11-22 21:19:09

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

如何在if else PHP语句中使用'和/或' ?会:

1) AND

1)和

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

2) OR

2)或

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

10 个解决方案

#1


84  

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

是的。答案是肯定的。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.
  • $status = 'clear'应该是$status = 'clear'。= is赋值,== is比较。

#2


9  

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

在这种情况下,结果是“FALSE”,因为B不是1,现在如果。

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

这将返回“TRUE”,即使B仍然不是我们要求的值,也有另一种方法返回TRUE,而不使用OR / ||,那将是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

更多:http://www.php.net/manual/en/language.operators.logical.php

#3


8  

There's some joking, and misleading comments, even partially incorrect information in the answers here. I'd like to try to improve on them:

这里有一些玩笑和误导性的评论,甚至是部分错误的答案。我想试着改进一下:

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

首先,正如一些人指出的那样,您的代码中有一个与问题相关的错误:

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

should be (note the == instead of = in the first part):

应该是(注意==而不是=在第一部分):

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

which in this case is functionally equivalent to

在这个例子中,它在功能上等同于什么?

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

Second, note that these operators (and or && ||) are short-circuit operators. That means if the answer can be determined with certainty from the first expression, the second one is never evaluated. Again this doesn't matter for your debugged line above, but it is extremely important when you are combining these operators with assignments, because

第二,注意这些操作符(或&& & ||)是短路操作符。这意味着,如果答案可以从第一个表达式确定下来,第二个表达式永远不会被计算。这对于您的调试行来说并不重要,但是当您将这些操作符与任务组合在一起时,这是非常重要的,因为。

Third, the real difference between and or and && || is their operator precedence. Specifically the importance is that && || have higher precedence than the assignment operators (= += -= *= **= /= .= %= &= |= ^= <<= >>=) while and or have lower precendence than the assignment operators. Thus in a statement that combines the use of assignment and logical evaluation it matters which one you choose.

第三,||的真正区别在于它们的运算符优先级。特别的重要性是& & | |有高于赋值操作符的优先级(= + = - = * = * * = / =。= % = & = | = ^ = < < = > > =),或precendence低于赋值操作符。因此,在一个结合使用分配和逻辑评估的语句中,你选择哪一个是重要的。

Modified examples from PHP's page on logical operators:

在逻辑操作符上修改了PHP页面中的示例:

$e = false || true;

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

将计算为true并将其赋值为$e,因为||的运算符优先级高于=,因此它本质上是这样计算的:

$e = (false || true);

however

然而

$e = false or true;

will assign false to $e (and then perform the or operation and evaluate true) because = has higher operator precedence than or, essentially evaluating like this:

将false赋给$e(然后执行或运算并计算true),因为=具有更高的运算符优先级,或者,本质上是这样计算的:

($e = false) or true;

The fact that this ambiguity even exists makes a lot of programmers just always use && || and then everything works clearly as one would expect in a language like C, ie. logical operations first, then assignment.

这种模糊性的存在使得很多程序员总是使用&& ||,然后一切都很清晰,就像在C语言中所期望的那样。首先是逻辑操作,然后是赋值。

Some languages like Perl use this kind of construct frequently in a format similar to this:

像Perl这样的语言经常使用这种结构,类似于这样的格式:

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

This would theoretically assign the database connection to $connection, or if that failed (and we're assuming here the function would return something that evalues to false in that case), it will end the script with an error message. Because of short-circuiting, if the database connection succeeds, the die() is never evaluated.

理论上,这将把数据库连接分配给$connection,或者如果失败(我们假设函数会返回一些在这种情况下会导致错误的东西),它将以错误消息结束脚本。由于短路,如果数据库连接成功,die()就不会被计算。

Some languages that allow for this construct straight out forbid assignments in conditional/logical statements (like Python) to remove the amiguity the other way round.

允许这种构造的某些语言禁止在条件/逻辑语句(如Python)中进行赋值,以将amiguity移到另一个方向。

PHP went with allowing both, so you just have to learn about your two options once and then code how you'd like, but hopefully you'll be consistent one way or another.

PHP允许这两种方法,所以你只需要了解一下你的两个选项,然后编码你想要的,但希望你能以这样或那样的方式保持一致。

Whenever in doubt, just throw in an extra set of parenthesis, which removes all ambiguity. These will always be the same:

每当有疑问的时候,只要插入额外的括号,就可以消除所有的歧义。它们总是一样的:

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

Armed with all that knowledge, I prefer using and or because I feel that it makes the code more readable. I just have a rule not to combine assignments with logical evaluations. But at that point it's just a preference, and consistency matters a lot more here than which side you choose.

有了这些知识,我更喜欢使用,或者因为我觉得它让代码更容易读懂。我只是有一个规则,不把作业和逻辑评估结合起来。但在这一点上,这只是一种偏好,而一致性在这里比你选择的哪一边更重要。

#4


7  

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.

    使用&&为“和”和||为“或”。或者会工作,但他们是非常规的。

#5


5  

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

或者是||,比如C。

#6


4  

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

或者仅仅是语法上的糖和||,就像在JavaScript中一样,或者其他C风格的语法语言。

It appears AND and OR have lower precedence than their C style equivalents.

它出现或比C样式的等价物更低。

#7


1  

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");
} 

#8


0  

i think i am having a bit of confusion here. :) But seems no one else have ..

我想我在这里有点困惑。但是似乎其他人都没有……

Are you asking which one to use in this scenario? If Yes then And is the correct answer.

你是问在这个场景中使用哪一个?如果是,那就是正确答案。

If you are asking about how the operators are working, then

如果你问的是操作员是如何工作的。

In php both AND, && and OR, || will work in the same way. If you are new in programming and php is one of your first languages them i suggest using AND and OR, because it increases readability and reduces confusion when you check back. But if you are familiar with any other languages already then you might already have familiarized the && and || operators.

在php中,&&或,||将以同样的方式工作。如果你在编程方面是新手,而php是你的第一种语言,我建议你使用和使用,或者,因为它增加了可读性,并且在你检查时减少了混乱。但是如果您已经熟悉其他语言,那么您可能已经熟悉了&&和||操作符。

#9


0  

<?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 the 'else' with '&&' so both are placed ... argh

我用“&&”替换了“else”,所以它们都被放在了…啊

#10


-1  

"AND" does not work in my PHP code.

和“在我的PHP代码中不起作用”。

Server's version maybe?

也许服务器的版本?

"&&" works fine.

“& &”效果很好。

#1


84  

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

是的。答案是肯定的。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.
  • $status = 'clear'应该是$status = 'clear'。= is赋值,== is比较。

#2


9  

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

在这种情况下,结果是“FALSE”,因为B不是1,现在如果。

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

这将返回“TRUE”,即使B仍然不是我们要求的值,也有另一种方法返回TRUE,而不使用OR / ||,那将是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

更多:http://www.php.net/manual/en/language.operators.logical.php

#3


8  

There's some joking, and misleading comments, even partially incorrect information in the answers here. I'd like to try to improve on them:

这里有一些玩笑和误导性的评论,甚至是部分错误的答案。我想试着改进一下:

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

首先,正如一些人指出的那样,您的代码中有一个与问题相关的错误:

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

should be (note the == instead of = in the first part):

应该是(注意==而不是=在第一部分):

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

which in this case is functionally equivalent to

在这个例子中,它在功能上等同于什么?

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

Second, note that these operators (and or && ||) are short-circuit operators. That means if the answer can be determined with certainty from the first expression, the second one is never evaluated. Again this doesn't matter for your debugged line above, but it is extremely important when you are combining these operators with assignments, because

第二,注意这些操作符(或&& & ||)是短路操作符。这意味着,如果答案可以从第一个表达式确定下来,第二个表达式永远不会被计算。这对于您的调试行来说并不重要,但是当您将这些操作符与任务组合在一起时,这是非常重要的,因为。

Third, the real difference between and or and && || is their operator precedence. Specifically the importance is that && || have higher precedence than the assignment operators (= += -= *= **= /= .= %= &= |= ^= <<= >>=) while and or have lower precendence than the assignment operators. Thus in a statement that combines the use of assignment and logical evaluation it matters which one you choose.

第三,||的真正区别在于它们的运算符优先级。特别的重要性是& & | |有高于赋值操作符的优先级(= + = - = * = * * = / =。= % = & = | = ^ = < < = > > =),或precendence低于赋值操作符。因此,在一个结合使用分配和逻辑评估的语句中,你选择哪一个是重要的。

Modified examples from PHP's page on logical operators:

在逻辑操作符上修改了PHP页面中的示例:

$e = false || true;

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

将计算为true并将其赋值为$e,因为||的运算符优先级高于=,因此它本质上是这样计算的:

$e = (false || true);

however

然而

$e = false or true;

will assign false to $e (and then perform the or operation and evaluate true) because = has higher operator precedence than or, essentially evaluating like this:

将false赋给$e(然后执行或运算并计算true),因为=具有更高的运算符优先级,或者,本质上是这样计算的:

($e = false) or true;

The fact that this ambiguity even exists makes a lot of programmers just always use && || and then everything works clearly as one would expect in a language like C, ie. logical operations first, then assignment.

这种模糊性的存在使得很多程序员总是使用&& ||,然后一切都很清晰,就像在C语言中所期望的那样。首先是逻辑操作,然后是赋值。

Some languages like Perl use this kind of construct frequently in a format similar to this:

像Perl这样的语言经常使用这种结构,类似于这样的格式:

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

This would theoretically assign the database connection to $connection, or if that failed (and we're assuming here the function would return something that evalues to false in that case), it will end the script with an error message. Because of short-circuiting, if the database connection succeeds, the die() is never evaluated.

理论上,这将把数据库连接分配给$connection,或者如果失败(我们假设函数会返回一些在这种情况下会导致错误的东西),它将以错误消息结束脚本。由于短路,如果数据库连接成功,die()就不会被计算。

Some languages that allow for this construct straight out forbid assignments in conditional/logical statements (like Python) to remove the amiguity the other way round.

允许这种构造的某些语言禁止在条件/逻辑语句(如Python)中进行赋值,以将amiguity移到另一个方向。

PHP went with allowing both, so you just have to learn about your two options once and then code how you'd like, but hopefully you'll be consistent one way or another.

PHP允许这两种方法,所以你只需要了解一下你的两个选项,然后编码你想要的,但希望你能以这样或那样的方式保持一致。

Whenever in doubt, just throw in an extra set of parenthesis, which removes all ambiguity. These will always be the same:

每当有疑问的时候,只要插入额外的括号,就可以消除所有的歧义。它们总是一样的:

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

Armed with all that knowledge, I prefer using and or because I feel that it makes the code more readable. I just have a rule not to combine assignments with logical evaluations. But at that point it's just a preference, and consistency matters a lot more here than which side you choose.

有了这些知识,我更喜欢使用,或者因为我觉得它让代码更容易读懂。我只是有一个规则,不把作业和逻辑评估结合起来。但在这一点上,这只是一种偏好,而一致性在这里比你选择的哪一边更重要。

#4


7  

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.

    使用&&为“和”和||为“或”。或者会工作,但他们是非常规的。

#5


5  

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

或者是||,比如C。

#6


4  

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

或者仅仅是语法上的糖和||,就像在JavaScript中一样,或者其他C风格的语法语言。

It appears AND and OR have lower precedence than their C style equivalents.

它出现或比C样式的等价物更低。

#7


1  

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");
} 

#8


0  

i think i am having a bit of confusion here. :) But seems no one else have ..

我想我在这里有点困惑。但是似乎其他人都没有……

Are you asking which one to use in this scenario? If Yes then And is the correct answer.

你是问在这个场景中使用哪一个?如果是,那就是正确答案。

If you are asking about how the operators are working, then

如果你问的是操作员是如何工作的。

In php both AND, && and OR, || will work in the same way. If you are new in programming and php is one of your first languages them i suggest using AND and OR, because it increases readability and reduces confusion when you check back. But if you are familiar with any other languages already then you might already have familiarized the && and || operators.

在php中,&&或,||将以同样的方式工作。如果你在编程方面是新手,而php是你的第一种语言,我建议你使用和使用,或者,因为它增加了可读性,并且在你检查时减少了混乱。但是如果您已经熟悉其他语言,那么您可能已经熟悉了&&和||操作符。

#9


0  

<?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 the 'else' with '&&' so both are placed ... argh

我用“&&”替换了“else”,所以它们都被放在了…啊

#10


-1  

"AND" does not work in my PHP code.

和“在我的PHP代码中不起作用”。

Server's version maybe?

也许服务器的版本?

"&&" works fine.

“& &”效果很好。