pbootcms网站模板|日韩1区2区|织梦模板||网站源码|日韩1区2区|jquery建站特效-html5模板网

    <small id='znTCB'></small><noframes id='znTCB'>

    1. <i id='znTCB'><tr id='znTCB'><dt id='znTCB'><q id='znTCB'><span id='znTCB'><b id='znTCB'><form id='znTCB'><ins id='znTCB'></ins><ul id='znTCB'></ul><sub id='znTCB'></sub></form><legend id='znTCB'></legend><bdo id='znTCB'><pre id='znTCB'><center id='znTCB'></center></pre></bdo></b><th id='znTCB'></th></span></q></dt></tr></i><div class="acomkec" id='znTCB'><tfoot id='znTCB'></tfoot><dl id='znTCB'><fieldset id='znTCB'></fieldset></dl></div>

      1. <legend id='znTCB'><style id='znTCB'><dir id='znTCB'><q id='znTCB'></q></dir></style></legend><tfoot id='znTCB'></tfoot>

          <bdo id='znTCB'></bdo><ul id='znTCB'></ul>

        處理 php 頁面錯誤的最佳方法?

        Best way to handle errors on a php page?(處理 php 頁面錯誤的最佳方法?)

            <tbody id='QcBsD'></tbody>
            <bdo id='QcBsD'></bdo><ul id='QcBsD'></ul>
          • <tfoot id='QcBsD'></tfoot>

              <legend id='QcBsD'><style id='QcBsD'><dir id='QcBsD'><q id='QcBsD'></q></dir></style></legend>
                • <small id='QcBsD'></small><noframes id='QcBsD'>

                • <i id='QcBsD'><tr id='QcBsD'><dt id='QcBsD'><q id='QcBsD'><span id='QcBsD'><b id='QcBsD'><form id='QcBsD'><ins id='QcBsD'></ins><ul id='QcBsD'></ul><sub id='QcBsD'></sub></form><legend id='QcBsD'></legend><bdo id='QcBsD'><pre id='QcBsD'><center id='QcBsD'></center></pre></bdo></b><th id='QcBsD'></th></span></q></dt></tr></i><div class="0uwgcme" id='QcBsD'><tfoot id='QcBsD'></tfoot><dl id='QcBsD'><fieldset id='QcBsD'></fieldset></dl></div>
                • 本文介紹了處理 php 頁面錯誤的最佳方法?的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

                  問題描述

                  現在我的頁面看起來像這樣:

                  Right now my pages look something like this:

                  if($_GET['something'] == 'somevalue')
                  {
                      $output .= 'somecode';
                  
                      // make a DB query, fetch a row
                      //...
                      $row = $stmt->Fetch(PDO::ASSOC);
                  
                      if($row != null)
                      {
                          $output .= 'morecode';
                  
                          if(somethingIsOK())
                          {
                              $output .= 'yet more page output';
                          }
                          else
                          {
                              $error = 'something is most definitely not OK.';
                          }
                      }
                      else
                      {
                          $error = 'the row does not exist.';
                      }
                  }
                  else
                  {
                      $error = 'something is not a valid value';
                  }
                  
                  if($error == '') // no error
                  {
                      //display $output on page
                  }
                  else // an error
                  {
                      // display whatever error occurred on the page
                  }
                  

                  我做事的方式有效,但對于可能顯而易見的事情來說非常繁瑣和乏味:假設我在代碼中間的某個地方調用了一個函數,或者想要檢查一個變量的值,或者驗證一個數據庫查詢返回了一個有效的結果,如果失敗我想輸出一個錯誤?我將不得不制作另一個 if/else 塊并將所有代碼移動到新的 if 塊中.這似乎不是一種聰明的做事方式.

                  The way I'm doing things works, but it's very cumbersome and tedious for what is probably obvious: suppose that I call a function somewhere in the middle of my code, or want to check the value of a variable, or verify a DB query returned a valid result, and if it fails I want to output an error? I would have to make another if/else block and move all of the code inside the new if block. This doesn't seem like a smart way of doing things.

                  我一直在閱讀有關 try/catch 的文章,并且一直在考慮將我的所有代碼放在一個 try 語句中,然后讓代碼按順序運行而沒有任何 if/else 塊,如果某些事情失敗則拋出異常.從我讀過的內容來看,這將停止執行并使其直接跳轉到 catch 塊(就像失敗的 if 語句將轉到 else 塊一樣),然后我可以在那里輸出錯誤消息.但這是可接受的或標準的做法嗎?

                  I have been reading about try/catch and have been thinking of putting all of my code inside a try statement, then let the code run sequentially without any if/else blocks and if something fails just throw an exception. From what I've read, that would halt the execution and make it jump straight to the catch block (just as a failed if statement will go to the else block), where I could then output the error message. But is that an acceptable or standard practice?

                  在構建和輸出 HTML 頁面的 php 應用程序中,處理錯誤的最佳方式是什么?我不想死在一個空白屏幕上,因為這對用戶非常不友好,而是想在頁面正文中輸出一條消息,仍然允許顯示頁眉和頁腳.

                  What's the best way of handling errors, fatal or not, in a php application that builds and outputs an HTML page? I don't want to just die with a blank screen, as that would be very user un-friendly, but instead want to output a message in the body of the page, still allowing the header and footer to show.

                  感謝您的建議!

                  推薦答案

                  有很多方法可以解決這個問題,坦率地說,沒有一種方法本質上是正確的".

                  There are a lot of ways that you can deal with this and frankly none of them is intrinsically 'right'.

                  您必須自己決定哪種方法對您來說更舒服" - 這始終取決于偏好(盡管您應該避免使用某些技術并且有充分的理由).

                  You will have to decide for yourself, which method is more 'comfortable' for you - it's always a mater of preferences (although there are certain techniques you should avoid and for good reasons).

                  這在很大程度上取決于您如何拆分邏輯,但是我傾向于將所有可以返回非致命錯誤的代碼包含在函數中,并使用所述函數的返回值來指示存在錯誤.

                  It will highly depend on how you split your logic, however I tend to enclose all code that can return non-fatal errors inside a function, and use a return value of said function to indicate there was an error.

                  對于致命錯誤,我傾向于使用異常(使用try-catch 塊).

                  For fatal errors I tend to use exceptions (with try-catch blocks).

                  現在澄清一下:

                  • 非致命錯誤是您可以恢復的錯誤 - 這意味著即使出現問題,仍有一些代碼可以執行并生成一些有價值的輸出.例如,如果您想使用NTP 協議獲取當前時間,但服務器沒有響應,您可以決定使用本地time 功能并仍然顯示一些有價值的數據給用戶.
                  • 致命錯誤是一種您無法恢復的錯誤 - 意味著發生了非常糟糕的事情,您唯一能做的就是告訴您的用戶頁面無法執行原來的操作被要求做.例如,如果您從數據庫中獲取一些數據并遇到 SQL Exception - 則不會顯示任何有價值的數據,您只能將此通知給用戶.
                  • A non-fatal error is an error that you can recover from - meaning that even though something went wrong, there is still some code that can be executed and generate some valuable output. For example if you wanted to get current time using NTP protocol, but the server didn't respond, you can decide to use local time function and still display a some valuable data to the user.
                  • A fatal error is an error that you would not be able to recover from - meaning that something really bad happened and the only thing you can do is tell your user that page cannot do what it was asked to. For example if you were fetching some data from your database and got SQL Exception - there is no valuable data to be shown and you can only inform the user of this.

                  使用函數返回作為處理非致命問題的一個很好的例子是一個函數試圖在頁面上顯示某個文件的內容當這不是頁面的主要目標時(例如,您將有一個功能可以在每一頁上顯示從文本文件中獲取的徽章 - 我知道這有點牽強,但請耐心等待).

                  A good example of using function-returns as a way of dealing with non-fatal problems would be a function that is trying to display content of some file on the page when this is not the main objective of the page (for example you would have a function that displays badges, fetched from a text file, on every single page - I know that this is far fetched but bear with me).

                  function getBadge($file){
                      $f = fopen($file,'r');
                      if(!$f){
                          return null;
                      }
                      .. do some processing ..
                      return $badges;
                  }
                  
                  $badges = getBadges('badges.txt');
                  if(!$badges){
                      echo "Cannot display badges.";
                  } else {
                      echo $badges;
                  }
                  .. carry on doing whatever page should be doing ..
                  

                  事實上,fopen 函數本身就是一個例子——它將返回.

                  In fact, the function fopen itself is an example of this - it will return.

                  成功時返回文件指針資源,錯誤時返回 FALSE.

                  Returns a file pointer resource on success, or FALSE on error.

                  <小時>

                  致命錯誤(使用異常 - try-catch)

                  當您有一些需要執行的代碼,因為它正是用戶想要的(例如從數據庫中讀取所有新聞并將它們顯示給用戶),您可以使用異常.讓我們舉一個簡單的例子 - 一個用戶訪問了他的個人資料并想查看他收到的所有消息(現在假設它們以純文本形式存儲).你可能有這樣的功能:


                  Fatal-Errors (using exceptions - try-catch)

                  When you have some piece of code that needs to be executed because it's exactly what the user wanted (for example reading all news from database and displaying them to the user), you could use exceptions. Let's take a simple example - a user visited his profile and wanted to see all the messages he's got (let's assume, for now, that they are stored in plain text). You might have a function like:

                  function getMessages($user){
                      $messages = array();
                      $f = fopen("messages_$user.txt","r");
                      if(!$f){
                          throw new Exception("Could not read messages!");
                      }
                      ... do some processing ...
                      return $messages;
                  }
                  

                  并像這樣使用它:

                  try{
                      ..do some stuff..
                      $messages = getMessages($_SESSION['user'])); //assuming you store username in $_SESSION
                      foreach($messages as $msg){
                          echo $msg."<br/>";
                      }
                  } catch(Exception $e){
                      echo "Sorry, there was an error: ".$e->getMessage();
                  }
                  

                  現在這可能會派上用場,如果您有一個可以執行所有其他代碼的頂級"腳本.這意味著,例如,在您的 index.php 中,您只需:

                  Now this could come in handy, if you had a 'top-level' script that would execute all the other code. That means that, for example, in your index.php you would just have:

                  try{
                      .. execute some code, perform some functions ..
                  } catch(Exception $e){
                      echo "Sorry, there was an error: ".$e->getMessage();
                  }
                  

                  不要過度使用異常!

                  無論您做什么,都不要使用異常來檢查可以從中恢復的內容.閱讀關于另一個問題(完全歸功于給 Anton Gogolev 一個很好的解釋,以及其他回答者)解釋為什么會這樣.

                  Do not overuse exceptions!

                  Whatever you do, never use exceptions as a way to check something you can recover from. Have a read on another question(full credit goes to Anton Gogolev for a very good explanation on this, as well as other answer-ers) as to why this is the case.

                  現在,沒有比嘗試多種方法并看看什么對您有好處更好的方法來學習如何處理錯誤了.您可能會發現以下內容很有用:

                  Now there is no better way to learn how to deal with errors than to try several things and see what is good for you. You might find the below useful:

                  • W3School 關于 PHP 異常處理
                  • 錯誤處理簡短教程(類似于我的函數返回方法)
                  • 有關 PHP 錯誤處理的詳盡教程 - 包括使用 trigger_error() 函數,我沒有提到它,因為我不使用它,也不太了解它,但顯然它真的很有用.這是一本特別好的讀物.
                  • W3School on PHP Exception handling
                  • Short tutorial on error handling(similar to my function-returns method)
                  • Extensive tutorial on PHP error handling - including using trigger_error() function, which I haven't mentioned because I don't use it and don't know much about it, but apparently it's really useful. This is a particularly good read.

                  希望這有幫助:)

                  這篇關于處理 php 頁面錯誤的最佳方法?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

                  【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!

                  相關文檔推薦

                  MySQLi prepared statement amp; foreach loop(MySQLi準備好的語句amp;foreach 循環)
                  Is mysqli_insert_id() gets record from whole server or from same user?(mysqli_insert_id() 是從整個服務器還是從同一用戶獲取記錄?)
                  PHP MySQLi doesn#39;t recognize login info(PHP MySQLi 無法識別登錄信息)
                  mysqli_select_db() expects exactly 2 parameters(mysqli_select_db() 需要 2 個參數)
                  Php mysql pdo query: fill up variable with query result(Php mysql pdo 查詢:用查詢結果填充變量)
                  MySQLI 28000/1045 Access denied for user #39;root#39;@#39;localhost#39;(MySQLI 28000/1045 用戶“root@“localhost的訪問被拒絕)

                    <tbody id='DgEFE'></tbody>

                • <tfoot id='DgEFE'></tfoot>
                  <i id='DgEFE'><tr id='DgEFE'><dt id='DgEFE'><q id='DgEFE'><span id='DgEFE'><b id='DgEFE'><form id='DgEFE'><ins id='DgEFE'></ins><ul id='DgEFE'></ul><sub id='DgEFE'></sub></form><legend id='DgEFE'></legend><bdo id='DgEFE'><pre id='DgEFE'><center id='DgEFE'></center></pre></bdo></b><th id='DgEFE'></th></span></q></dt></tr></i><div class="ec2qgeo" id='DgEFE'><tfoot id='DgEFE'></tfoot><dl id='DgEFE'><fieldset id='DgEFE'></fieldset></dl></div>
                    <bdo id='DgEFE'></bdo><ul id='DgEFE'></ul>

                    <small id='DgEFE'></small><noframes id='DgEFE'>

                        • <legend id='DgEFE'><style id='DgEFE'><dir id='DgEFE'><q id='DgEFE'></q></dir></style></legend>
                          1. 主站蜘蛛池模板: 首页-恒温恒湿试验箱_恒温恒湿箱_高低温试验箱_高低温交变湿热试验箱_苏州正合 | 自动气象站_气象站监测设备_全自动气象站设备_雨量监测站-山东风途物联网 | 上海恒驭仪器有限公司-实验室平板硫化机-小型平板硫化机-全自动平板硫化机 | 济南ISO9000认证咨询代理公司,ISO9001认证,CMA实验室认证,ISO/TS16949认证,服务体系认证,资产管理体系认证,SC食品生产许可证- 济南创远企业管理咨询有限公司 郑州电线电缆厂家-防火|低压|低烟无卤电缆-河南明星电缆 | 电动葫芦|防爆钢丝绳电动葫芦|手拉葫芦-保定大力起重葫芦有限公司 | 高压贴片电容|贴片安规电容|三端滤波器|风华电容代理南京南山 | 分子精馏/精馏设备生产厂家-分子蒸馏工艺实验-新诺舜尧(天津)化工设备有限公司 | 电脑刺绣_绣花厂家_绣花章仔_织唛厂家-[源欣刺绣]潮牌刺绣打版定制绣花加工厂家 | 科昊仪器超纯水机系统-可成气相液氮罐-美菱超低温冰箱-西安昊兴生物科技有限公司 | 创客匠人-让IP变现不走弯路| 上海公众号开发-公众号代运营公司-做公众号的公司企业服务商-咏熠软件 | 通信天线厂家_室分八木天线_对数周期天线_天线加工厂_林创天线源头厂家 | 筛分机|振动筛分机|气流筛分机|筛分机厂家-新乡市大汉振动机械有限公司 | elisa试剂盒价格-酶联免疫试剂盒-猪elisa试剂盒-上海恒远生物科技有限公司 | 柴油机_柴油发电机_厂家_品牌-江苏卡得城仕发动机有限公司 | 碳化硅,氮化硅,冰晶石,绢云母,氟化铝,白刚玉,棕刚玉,石墨,铝粉,铁粉,金属硅粉,金属铝粉,氧化铝粉,硅微粉,蓝晶石,红柱石,莫来石,粉煤灰,三聚磷酸钠,六偏磷酸钠,硫酸镁-皓泉新材料 | 塑料检查井_双扣聚氯乙烯增强管_双壁波纹管-河南中盈塑料制品有限公司 | 快干水泥|桥梁伸缩缝止水胶|伸缩缝装置生产厂家-广东广航交通科技有限公司 | 石栏杆_青石栏杆_汉白玉栏杆_花岗岩栏杆 - 【石雕之乡】点石石雕石材厂 | 体视显微镜_荧光生物显微镜_显微镜报价-微仪光电生命科学显微镜有限公司 | 澳门精准正版免费大全,2025新澳门全年免费,新澳天天开奖免费资料大全最新,新澳2025今晚开奖资料,新澳马今天最快最新图库-首页-东莞市傲马网络科技有限公司 | 安全阀_弹簧式安全阀_美标安全阀_工业冷冻安全阀厂家-中国·阿司米阀门有限公司 | 快干水泥|桥梁伸缩缝止水胶|伸缩缝装置生产厂家-广东广航交通科技有限公司 | Eiafans.com_环评爱好者 环评网|环评论坛|环评报告公示网|竣工环保验收公示网|环保验收报告公示网|环保自主验收公示|环评公示网|环保公示网|注册环评工程师|环境影响评价|环评师|规划环评|环评报告|环评考试网|环评论坛 - Powered by Discuz! | 专业生产动态配料系统_饲料配料系统_化肥配料系统等配料系统-郑州鑫晟重工机械有限公司 | 澳洁干洗店加盟-洗衣店干洗连锁「澳洁干洗免费一对一贴心服务」 干洗加盟网-洗衣店品牌排行-干洗设备价格-干洗连锁加盟指南 | 不锈钢搅拌罐_高速搅拌罐厂家-无锡市凡格德化工装备科技有限公司 | 全自动在线分板机_铣刀式在线分板机_曲线分板机_PCB分板机-东莞市亿协自动化设备有限公司 | Magnescale探规,Magnescale磁栅尺,Magnescale传感器,Magnescale测厚仪,Mitutoyo光栅尺,笔式位移传感器-苏州连达精密量仪有限公司 | 烟台游艇培训,威海游艇培训-烟台市邮轮游艇行业协会 | 重庆私家花园设计-别墅花园-庭院-景观设计-重庆彩木园林建设有限公司 | 油漆辅料厂家_阴阳脚线_艺术漆厂家_内外墙涂料施工_乳胶漆专用防霉腻子粉_轻质粉刷石膏-魔法涂涂 | 工作心得_读书心得_学习心得_找心得体会范文就上学道文库 | 雨燕360体育免费直播_雨燕360免费NBA直播_NBA篮球高清直播无插件-雨燕360体育直播 | 掺铥光纤放大器-C/L波段光纤放大器-小信号光纤放大器-合肥脉锐光电技术有限公司 | 氢氧化钾厂家直销批发-济南金昊化工有限公司 | 环氧树脂地坪_防静电地坪漆_环氧地坪漆涂料厂家-地壹涂料地坪漆 环球电气之家-中国专业电气电子产品行业服务网站! | 布袋除尘器|除尘器设备|除尘布袋|除尘设备_诺和环保设备 | 济南品牌包装设计公司_济南VI标志设计公司_山东锐尚文化传播 | 流量检测仪-气密性检测装置-密封性试验仪-东莞市奥图自动化科技有限公司 | 水压力传感器_数字压力传感器|佛山一众传感仪器有限公司|首页 |