問題描述
現在我的頁面看起來像這樣:
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 localtime
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模板網!