“做某事 OR DIE()”如何在 PHP 中工作?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/432454/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 22:43:43  来源:igfitidea点击:

How does "do something OR DIE()" work in PHP?

phpmysqlexception-handlingconditional

提问by chustar

I'm writing a php app to access a MySQL database, and on a tutorial, it says something of the form

我正在编写一个 php 应用程序来访问 MySQL 数据库,并且在一个教程中,它说明了一些形式

mysql_connect($host, $user, $pass) or die("could not connect");

How does PHP know that the function failed so that it runs the die part? I guess I'm asking how the "or" part of it works. I don't think I've seen it before.

PHP 如何知道该函数失败以运行模具部分?我想我在问它的​​“或”部分是如何工作的。我想我以前没见过。

回答by nickf

If the first statement returns true, then the entire statement must be truetherefore the second part is never executed.

如果第一条语句返回true,则整个语句一定是true因此第二部分永远不会执行。

For example:

例如:

$x = 5;
true or $x++;
echo $x;  // 5

false or $x++;
echo $x; // 6

Therefore, if your query is unsuccessful, it will evaluate the die()statement and end the script.

因此,如果您的查询不成功,它将评估die()语句并结束脚本。

回答by Artelius

PHP's orworks like C's ||(which incidentally is also supported by PHP - orjust looks nicer and has different operator precedence - see this page).

PHP 的or工作方式类似于 C ||(顺便说一句,PHP 也支持它 -or只是看起来更好,并且具有不同的运算符优先级 - 请参阅此页面)。

It's known as a short-circuitoperator because it will skip any evaluations once it has enough information to decide the final value.

它被称为短路运算符,因为一旦它有足够的信息来决定最终值,它就会跳过任何评估。

In your example, if mysql_connect()returns TRUE, then PHP already knows that the whole statement will evaluate to TRUE no matter what die()evalutes to, and hence die()isn't evaluated.

在您的示例中,如果mysql_connect()返回 TRUE,则 PHP 已经知道无论计算什么,整个语句都将计算为 TRUE die(),因此die()不会对其进行评估。

If mysql_connect()returns FALSE, PHP doesn't know whether the whole statement will evaluate to TRUE or FALSE so it goes on and tries to evalute die()- ending the script in the process.

如果mysql_connect()返回 FALSE,PHP 不知道整个语句的计算结果是 TRUE 还是 FALSE,因此它继续并尝试计算die()- 结束该过程中的脚本。

It's just a nice trick that takes advantage of the way orworks.

这只是一个利用工作方式的好技巧or

回答by MarkR

It works as others have described.

它像其他人描述的那样工作。

In PHP, do not use "die", as it does NOT raise an exception (as it does in Perl). Instead throw an exception properly in the normal way.

在 PHP 中,不要使用“die”,因为它不会引发异常(就像在 Perl 中一样)。而是以正常方式正确抛出异常。

die cannot be caught in PHP, and does not log - instead it prints the message ungracefully and immediately quits the script without telling anybody anything or giving you any opportunity to record the event, retry etc.

die 无法在 PHP 中捕获,也不会记录 - 相反,它会不雅地打印消息并立即退出脚本,而不告诉任何人任何事情或给您任何机会记录事件、重试等。

回答by Arun Kumar

$con=mysql_connect($host, $user, $pass)
if(!$con)
{
     die("could not connect");
}
else
{
     echo "Connected";
}