如何在出错时使用自定义html消息退出php

如何在出错时使用自定义html消息退出php,第1张

概述我想知道什么是退出 PHP脚本的最佳方式(如果我遇到错误),我也包括我所有的HTML代码.我的剧本是: <?php // Some other code here // These if statements are my error handling if(!isset($var)) { $message = 'Var is not set' 我想知道什么是退出 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页面,然后使用str_replace函数替换HTML页面中的关键字.在这种情况下,我们用您的错误消息替换的单词是{message}.

error_page_template.HTML

<!DOCTYPE HTML><HTML>    <head>        <Title>Error Page</Title>    </head>    <body>        {message}    </body></HTML>

script.PHP的

<?PHP    function error_page($message) {        $HTMLTemplate = file_get_contents('error_page_template.HTML');        $errorPage = str_replace('{message}',$message,$HTMLTemplate);        return $errorPage;    }    echo error_page('An error has occurred');?>
总结

以上是内存溢出为你收集整理的如何在出错时使用自定义html消息退出php全部内容,希望文章能够帮你解决如何在出错时使用自定义html消息退出php所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/web/1071319.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-05-26
下一篇2022-05-26

发表评论

登录后才能评论

评论列表(0条)

    保存