PHP if OR 第二部分检查是否为真?

发布于 2024-11-25 09:14:45 字数 293 浏览 1 评论 0原文

我知道这一定是一个简单的问题,但我知道在 PHP 中这样的语句中

if ($a && $b) { do something }

if $a is false PHP 甚至不检查 $b

那么 OR 也是如此,所以

if ($a || $b) { do something }

如果 $a 为 true,那么它会仍然检查 $b

我知道这是基本的东西,但我无法在任何地方找到答案......谢谢

I know this must be a simple question, but I know that in PHP in a statement like this

if ($a && $b) { do something }

if $a is false PHP doesn't even check $b

Well is the same thing true about OR so

if ($a || $b) { do something }

If $a is true, does it still check $b

I know this is elementary stuff, but I can't find the answer anywhere... Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

心安伴我暖 2024-12-02 09:14:45

一旦知道结果,逻辑表达式的计算就会停止。

逻辑运算符

Evaluation of logical expressions is stopped as soon as the result is known.

logical operators

情域 2024-12-02 09:14:45

请参阅手册中逻辑运算符页面上的示例 1。

// --------------------
// foo() will never get called as those operators are short-circuit

$a = (false && foo());
$b = (true  || foo());
$c = (false and foo());
$d = (true  or  foo());

See Example 1 on the Logical Operators page in the manual.

// --------------------
// foo() will never get called as those operators are short-circuit

$a = (false && foo());
$b = (true  || foo());
$c = (false and foo());
$d = (true  or  foo());
新雨望断虹 2024-12-02 09:14:45

看这个例子:

function foo1() {
  echo "blub1\n";
  return true;
}

function foo2() {
  echo "blub2\n";
  return false;
}

if (foo1() || foo2()) {
  echo "end.";
}

$b / foo2() 没有被检查。
此处演示:codepad.org

Look at this example:

function foo1() {
  echo "blub1\n";
  return true;
}

function foo2() {
  echo "blub2\n";
  return false;
}

if (foo1() || foo2()) {
  echo "end.";
}

$b / foo2() isnt checked.
Demo here: codepad.org

不交电费瞎发啥光 2024-12-02 09:14:45

如果至少一个 OR 操作数为 true,则无需进一步检查其他操作数,整个结果将评估为 true。

如果至少有一个操作数为真, (a || b || c || d || e ||...) 将为 TRUE,因此一旦我发现一个操作数为 true,我就不需要检查以下内容操作数。

这个逻辑无处不在,PHP、JAVA、C...

If at least one OR Operand is true, there is no need to go further and check the other operands and the whole thing will evaluate to true.

(a || b || c || d || e ||...) will be TRUE if at least one of the operands is true, thus once I found one operand to be true I do not need to check the following operands.

This logic applies everywhere, PHP, JAVA, C...

○闲身 2024-12-02 09:14:45

如果您非常了解真值表,那么您可能可以自己算出来。正如其他人所说,PHP 将进行评估,直到确定结果为止。在 OR 的情况下,只有其中一个为 true 语句才能返回 true。因此 PHP 会进行计算,直到找到一个真正的值。如果没有找到,则该语句的计算结果为 false。

<?php
if(true && willGetCalled()) {}
if(false && wontGetCalled()) {}
if(true || wontGetCalled()) {}
if(false || willGetCalled()) {}
?>

If you know your truth tables fairly well, then you can probably figure it out yourself. As others have said, PHP will evaluate until it is certain of an outcome. In the case of OR, only one has to be true for the statement to return true. So PHP evaluates until it finds a true value. If it doesn't find one, the statement evaluates to false.

<?php
if(true && willGetCalled()) {}
if(false && wontGetCalled()) {}
if(true || wontGetCalled()) {}
if(false || willGetCalled()) {}
?>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文