“或”的替代方案PHP 中的关键字
现在我有以下代码:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
带“or”的语句有效,因为 PHP 解释器非常智能:
因为“或”的连接为真,所以如果其中第一个为真,则当第一个为真时,它将停止执行该语句。
想象一下下面的(PHP)代码:
现在您可以链接函数调用并在输出中查看会发生什么:
只会告诉您“_true”,因为链在第一个为真后结束,其他两个将永远不会被执行。
将给出“_false_true”,因为第一个函数返回 false 并且解释器继续。
对“and”也有同样的效果
您也可以对“and”做同样的事情,不同之处在于,当第一个“false”出现时,“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:
now you can chain the function calls and see in the output what happens:
will tell you only "_true", because the chain is ended after the first one was true, the other two will never be executed.
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:
will echo "_false" because there the result is already finished an cannot be changed anymore.
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()
.逻辑上相当于
所以你的解决方案很好。
is logically equivalent to
So your solution is fine.
由于
stream_wrapper_register()
返回一个布尔值,因此完全相同。It's exactly the same since
stream_wrapper_register()
returns a boolean value.这两种语法是等效的,只是使用哪一种的编码风格/偏好问题。就我个人而言,我不喜欢
;或 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.有一个“OR”运算符可用。
符号是||
因此,举个例子考虑:
THere an "OR" operator available.
the sign is ||
hence, as an example consider: