“或”的替代方案PHP 中的关键字

发布于 2025-01-01 17:16:28 字数 746 浏览 0 评论 0原文

现在我有以下代码:

stream_wrapper_register("var", "VariableStream")
    or die("Failed to register protocol");

我想在死之前做一些额外的事情,以防函数失败。所以它提出了这个问题:

“or”关键字到底是如何工作的?

在许多SO问题或答案中,我看到人们创建了一个函数,如下所示:

function doStuff() {
    header('HTTP/1.1 500 Internal Server Error');
    die("Failed to register protocol");
}

stream_wrapper_register("var", "VariableStream")
    or doStuff();

...但这有点在面向对象的上下文中不切实际,因为我真的不想在我的对象中创建一个方法,而且我还不能使用闭包。

所以现在,我已经使用了这段代码,但我不确定它是否会具有完全相同的行为:

if (!stream_wrapper_register("var", "VariableStream") {
    header('HTTP/1.1 500 Internal Server Error');
    die("Failed to register protocol");
}

Right now I have the following code:

stream_wrapper_register("var", "VariableStream")
    or die("Failed to register protocol");

And I want to do extra stuff before the die in case the function fail. So it raised that question :

How does the 'or' keyword works exactly ?

In many SO questions or answer, I've seen people creating a function, like this :

function doStuff() {
    header('HTTP/1.1 500 Internal Server Error');
    die("Failed to register protocol");
}

stream_wrapper_register("var", "VariableStream")
    or doStuff();

... but this is kind of unpractical in an Object Oriented context as I don't really want to create a method for that in my object, and I can't yet use closure.

So for now, I've used this code, but I'm not sure that it will have the exact same behaviour :

if (!stream_wrapper_register("var", "VariableStream") {
    header('HTTP/1.1 500 Internal Server Error');
    die("Failed to register protocol");
}

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

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

发布评论

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

评论(5

桃气十足 2025-01-08 17:16:28

带“or”的语句有效,因为 PHP 解释器非常智能:
因为“或”的连接为真,所以如果其中第一个为真,则当第一个为真时,它将停止执行该语句。

想象一下下面的(PHP)代码:

function _true()  { echo "_true";  return true;  }
function _false() { echo "_false"; return false; }

现在您可以链接函数调用并在输出中查看会发生什么:

_true() or _true() or _true();

只会告诉您“_true”,因为链在第一个为真后结束,其他两个将永远不会被执行。

_false() or _true() or _true();

将给出“_false_true”,因为第一个函数返回 false 并且解释器继续。

对“and”也有同样的效果

您也可以对“and”做同样的事情,不同之处在于,当第一个“false”出现时,“and”链就完成了:

_false() and _true() and _true();

将回显“_false”因为结果已经完成并且不能再更改。

_true() and _true() and _false();

将写“_true_true_false”。

因为大多数函数通过成功时返回“1”和错误时返回 0 来指示其成功,因此您可以执行诸如 function() 或 die() 之类的操作。
但有些函数(在 php 中很少)在成功时返回“0”,并且 != 0 表示特定错误。那么你可能必须使用function()和die()

The statement with "or" works, because the PHP-Interpreter is quite intelligent:
Because a connection of "or"'s is true, if the first of them is true, it stops executing the statement when the first one is true.

Imagine following (PHP)code:

function _true()  { echo "_true";  return true;  }
function _false() { echo "_false"; return false; }

now you can chain the function calls and see in the output what happens:

_true() or _true() or _true();

will tell you only "_true", because the chain is ended after the first one was true, the other two will never be executed.

_false() or _true() or _true();

will give "_false_true" because the first function returns false and the interpreter goes on.

The same works with "and"

You can also do the same with "and", with the difference that a chain of "and"'s is finished, when the first "false" occurres:

_false() and _true() and _true();

will echo "_false" because there the result is already finished an cannot be changed anymore.

_true() and _true() and _false();

will write "_true_true_false".

Because most of all functions indicate their success by returning "1" on success and 0on error you can do stuff like function() or die().
But some functions (in php quite seldom) return "0" on succes, and != 0 to indicate a specific error. Then you may have to use function() and die().

与风相奔跑 2025-01-08 17:16:28
a or b

逻辑上相当于

if (!a) {
    b
}

所以你的解决方案很好。

a or b

is logically equivalent to

if (!a) {
    b
}

So your solution is fine.

止于盛夏 2025-01-08 17:16:28

由于 stream_wrapper_register() 返回一个布尔值,因此完全相同。

It's exactly the same since stream_wrapper_register() returns a boolean value.

海螺姑娘 2025-01-08 17:16:28

这两种语法是等效的,只是使用哪一种的编码风格/偏好问题。就我个人而言,我不喜欢 ;或 die(...),因为在大多数情况下它似乎更难阅读。

The two syntaxes are equivalent, and it's just a matter of coding style/preference as to which one you use. Personally, I don't like <statement> or die(...), as it seems harder to read in most circumstances.

回梦 2025-01-08 17:16:28

有一个“OR”运算符可用。
符号是||

因此,举个例子考虑:

<?php
$count = NULL;

if ($count == 0 || $count == NULL) {
        echo "\nNo Value!\n";
        var_dump($count);
    } else {
        echo "\nSome Value";
    }
?>

THere an "OR" operator available.
the sign is ||

hence, as an example consider:

<?php
$count = NULL;

if ($count == 0 || $count == NULL) {
        echo "\nNo Value!\n";
        var_dump($count);
    } else {
        echo "\nSome Value";
    }
?>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文