dtra49684 2013-05-06 00:35
浏览 10
已采纳

如何让这个PHP函数更高效?

I want to find out a more efficient way of going about this function:

<?php
function zipError($title, $desc) {
    echo '<style type="text/css">';
    echo 'body { margin:0;font-family:Arial, sans-serif;font-size:13px;background:#DDD;color:#1A1A1A; }';
    echo 'h1 { color:#FFF;font-family:Arial;font-weight:bold;text-align:center; }';
    echo 'h2 { color:#1C1C1C;font-family:Arial;font-weight:bold;text-align:center;padding-bottom:4px;border-bottom:1px solid #AAA; }';
    echo '#error#head { background:#1C1C1C;padding:10px;;border-bottom:5px solid #FFF; }';
    echo '#error#content { border:1px solid #AAA;background:#FFF;padding:20px;width:780px;margin:30px auto; }';
    echo 'p { text-align:center;padding:10px;color:#1A1A1A;font-family:verdana; }';
    echo '</style>';
    echo '<title>' . $title . '</title>';
    echo '<div id="error head"><h1>An error has occurred.</h1></div>';
    echo '<div id="error content"><h2>'.$title.'</h2><p>'.$desc.'</p></div>';
}

This code is used to throw an error. For example:

die(zipError('Session Not Found', 'Your session has not been found! Please re-login now!'));

Although this gets the job done, I'm trying to learn, so therefore I want to make this function more efficient, and not hard coded. Any ideas?

  • 写回答

3条回答 默认 最新

  • douyuan9512 2013-05-06 01:12
    关注

    that's look like error template, I will do something like this:

    create sparate error tamplate: e.g error.php:

    <html>
       <head>
         <style>
          //style here...
         </style>
         <title><?= $title?></title>
       </head>
       <body>
           //and soon ...
       </body>
    

    and a function:

    public function zipError($title, $desc){
        include('error.php');
    }
    

    and usage:

     ... die(zipError('Session Not Found', 'Your session has not been found! Please re-login now!'));
    

    Now, You can edit/change your error template easily as long as it keep echoing $title and $desc and any other parameter you want to.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 用windows做服务的同志有吗
  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 一直显示正在等待HID—ISP