抛出 die() 时指定页/行?

发布于 2024-07-15 23:11:30 字数 133 浏览 5 评论 0原文

我使用的是 PHP 4,我知道导致错误并停止一切的唯一方法是调用 die()。 但万一我稍后遇到错误并且不记得它来自哪里,我想指定 die() 发生的页面和行号(就像其他 php 错误一样)。 有没有办法做到这一点?

谢谢!

I am using PHP 4, the only way I know of to cause an error and stop everything is calling die(). But in case I run into the error later and don't remember where its coming from I would like to specify the page and line number that the die() occurred on (like other php errors do). Is there a way to do this?

Thanks!

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

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

发布评论

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

评论(5

热情消退 2024-07-22 23:11:30

您应该查看 魔法常量。

echo __LINE__; // Line number

您也可以运行error_log() 将错误安静地发送到日志。

You should look into the magic constants.

echo __LINE__; // Line number

You can also run error_log() to send errors quietly to the log.

暮年慕年 2024-07-22 23:11:30

我认为你应该使用 trigger_error() 生成 E_USER_ERROR或 E_USER_WARNING。 这使您可以详细控制行为。 例如,您可以使用 error_reporting() 指定是否应显示消息,或处理 E_USER_WARNING :s 显式使用 set_error_handler()。

I think you should use trigger_error() to generate an E_USER_ERROR or E_USER_WARNING. This allows you to control the behaviour in detail. For example you can specify whether the messages should be shown at all using error_reporting(), or handle the E_USER_WARNING:s explicitly using set_error_handler().

一世旳自豪 2024-07-22 23:11:30

最简单的方法是使用:

echo __FILE__ . ": line " . __LINE__;
die();

如果您要使用 PHP5,您还可以使用异常:

throw new Exception("My error message!");

堆栈跟踪将显示整个调用堆栈以及抛出此错误的行。

(编辑:感谢 [@John Isaacs] 和 [@Emil H] 通知我直到 PHP5 才将异常添加到 PHP)

The simplest way is to use:

echo __FILE__ . ": line " . __LINE__;
die();

If you were to use PHP5, you could also use Exceptions:

throw new Exception("My error message!");

The stack trace will reveal the whole call stack and the line this was thrown on.

(EDIT: Thanks to [@John Isaacs] and [@Emil H] for informing me that Exceptions weren't added to PHP until PHP5)

撩起发的微风 2024-07-22 23:11:30

除了 @Jukka Dahlbom 和 @Ólafur Waage 的建议之外,您还可以考虑使用 debug_backtrace()

In addition to @Jukka Dahlbom and @Ólafur Waage's suggestions you might also consider using debug_backtrace().

悸初 2024-07-22 23:11:30

最好使用 error_log() 报告错误并 debug_backtrace() 用于调试。 您还可以编写自己的错误处理函数(请参阅set_error_handler())将两者结合起来。

Better use error_log() to report an error and debug_backtrace() for debugging. You could also write your own error handling function (see set_error_handler()) to combine both.

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