简体   繁体   English

格式化die(); PHP消息

[英]Format die(); Message in PHP

Is there a way to style the output of php's die(); 有没有一种方法来样式化PHP的die();的输出die(); message? 信息?

I want to add some HTML and CSS around the error message so that I could use it in a productive environment. 我想在错误消息周围添加一些HTML和CSS,以便可以在生产环境中使用它。

If you're using the actual die() function, it will always print out exactly what text you pass it (which could be HTML, but might be awkward to use). 如果您使用的是实际的die()函数,它将始终准确打印出您传递给它的文本(可能是HTML,但使用起来很尴尬)。

You could, however, simply make your own function that prints out an error message nicely and then calls exit - that way you don't have to repeat the HTML that you might otherwise pass to die() every time. 但是,您可以简单地创建自己的函数,该函数可以很好地打印出错误消息,然后调用exit这样,您就不必重复否则可能每次都传递给die()的HTML。

function die_nicely($msg) {
    echo <<<END
<div id="critical_error">$msg</div>
END;
    exit;
}
<?php
if('1'=='1')
echo '<font color=red>';
die('Its true');
echo 'its false';
?>

   <?php
    if('1'=='1')
   {    
      echo '<font color=red>Itss true too.</font>';
      exit();
   }
    echo 'its false';
    ?>

Both above are working, just to clear your doubts. 以上两种方法都可以正常工作,只是为了消除您的疑虑。 :) :)

您可以将html添加到die的字符串中,但更简单的方法是在调用die之前回显所需的html。

是的,你可以这样

die("<div>Error: ".mysql_error()."</div>");

为什么不将die()更改为其包装器?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM