【发布时间】:2019-05-31 13:24:10
【问题描述】:
我想知道退出 php 脚本(如果遇到错误)的最佳方式是什么,我还包括我的所有 html 代码。目前我的脚本是:
<?php
// Some other code here
// These if statements are my error handling
if(!isset($var)) {
$message = 'Var is not set'
exit('<title>Error Page</title>'.$message.'<footer>Test Footer</foot>');
}
if($a != $b) {
$message = 'a is not equal to b';
exit('<title>Error Page</title>'.$message.'<footer>Test Footer</foot>');
}
$success = 'YAY, we made it to the end';
?>
<html>
<header>
<title>YAY</title>
<!-- Other stuff here -->
</header>
<!-- Other stuff here -->
<body>
<!-- Other stuff here -->
<?php echo $success ?>
<!-- Other stuff here -->
</body>
<!-- Other stuff here -->
<footer>The best footer</footer>
</html>
你可以看到我的退出消息的风格很糟糕(因为我把所有的 html 都塞在那里)。有没有一种方法可以让我有一个很好的 html 错误页面来显示自定义消息。
【问题讨论】:
-
但是你为什么要退出你的脚本呢?我可以看到你在 HTML 的上下文中运行它,所以你几乎不应该用退出来终止脚本
-
@Dharman 好吧,这是一个示例脚本,但退出的目的是因为我们遇到了错误,所以我想退出并显示一个漂亮的 html 格式的错误消息。您对处理错误有什么建议?
-
您还想打印底部的 html 部分吗?如果没有,那么您可以在
if中使用echo "your html code"; exit();。 -
@catcon 它们是示例 if 语句。在我的实际代码(更长)中,它有不同的 if 语句(正确地进行错误处理)。我不想发布长代码,因为这不是我要问的问题