有一種方法可以檢查「IF」如果函數在php中失敗?在PHP函數上檢查和錯誤
Ex。
If (getimagesize($image) returns and error) { echo 'Error: function dont work'; } else { // do something }
謝謝!
有一種方法可以檢查「IF」如果函數在php中失敗?在PHP函數上檢查和錯誤
Ex。
If (getimagesize($image) returns and error) { echo 'Error: function dont work'; } else { // do something }
謝謝!
我假設你對getimagesize
這樣的函數有特別的興趣,它不會返回任何錯誤代碼,並且對我們很難。但並非不可能。
的文檔getimagesize
狀態:
如果訪問的文件名的圖像是不可能的,或者如果它不是一個有效的圖片,
getimagesize()
將生成E_WARNING
電平的誤差。在讀取錯誤時,getimagesize()
將生成級別爲E_NOTICE
的錯誤。
因此,你需要做兩件事情:在其
可以實現使用set_error_handler()
和restore_error_handler()
的第一個。您可以通過error-control operator@
實現第二個。
因此,代碼必須是這樣的:
// Set our own error handler; we will restore the default one afterwards.
// Our new error handler need only handle E_WARNING and E_NOTICE, as per
// the documentation of getimagesize().
set_error_handler("my_error_handler", E_WARNING | E_NOTICE);
// No error has occured yet; it is the responsibility of my_error_handler
// to set $error_occurred to true if an error occurs.
$error_occurred = false;
// Call getimagesize; use operator @ to have errors not be generated
// However, your error handler WILL STILL BE CALLED, as the documentation
// for set_error_handler() states.
$size = @getimagesize(...);
// At this point, my_error_handler will have run if an error occurred, and
// $error_occurred will be true. Before doing anything with it, restore the
// previous error handler
restore_error_handler();
if($error_occurred) {
// whatever
}
else {
// no error; $size holds information we can use
}
function my_error_handler($errno, $errstr, $file, $line) {
global $error_occurred;
// If the code is written as above, then we KNOW that an error
// here was caused by getimagesize(). We also know what error it was:
switch($errno) {
case E_WARNING: // Access to image impossible, or not a valid picture
case E_NOTICE: // Read error
}
// We could also check what $file is and maybe do something based on that,
// if this error handler is used from multiple places. However, I would not
// recommend that. If you need more functionality, just package all of this
// into a class and use the objects of that class to store more state.
$error_occurred = true;
return true; // Do not let PHP's default error handler handle this after us
}
當然,這不是很維護(你有一個全局變量$error_occurred
那裏,這不是一個很好的做法)。因此,對於不僅可以正常工作而且設計得很好的解決方案,您可以將所有這些解決方案打包到一個課堂中。該類將定義:
my_error_handler
)的方法。要將對象方法設置爲錯誤處理程序而不是全局函數,則需要使用合適的第一個參數調用set_error_handler
;見the documentation for callback
。callback
以提供您的呼叫代碼和一組參數,然後使用call_user_func_array
執行此操作。如果在執行過程中調用了上面#1中設置的錯誤處理程序,請將其標記爲對象中的變量。您的方法會將返回值call_user_func_array
返回給調用代碼。那麼,如果那類叫做ErrorWatcher,調用代碼會是這樣的:
$watcher = new ErrorWatcher;
$size = $watcher->watch("getimagesize",
array(/* params for getimagesize here */));
// $size holds your result, if an error did not occur;
// check for errors and we 're done!
switch($watcher->report_last_error()) {
// error handling logic here
}
...這是好的,整齊不亂用全局變量。我希望我解釋得很好,可以讓你自己編寫類ErrorWatcher。 :-)
看看exceptions。儘管如此,你必須把它們放入你的函數中。
編輯:順便說一句,如果你的函數不應該返回一個布爾值,你可以總是有它返回false,如果出現錯誤和檢查,如:
$result = getimagesize($image);
if ($result === false)
{
//
}
無論條件是「if」語句(括號內的內容)將返回「true」或「false」。 如果條件返回「true」,則將執行第一條語句,否則將執行第二條語句。
你可以把這個,error_reporting(E_ALL);在你的腳本的最頂端,在你打開php標籤之後,看看你得到了什麼錯誤。
希望它有幫助。
也許是這樣的:
<?php
error_reporting(E_ALL);
more stuff here ...
if(your condition here){
echo "condition is true, do something";
}else{
echo "condition is not true, do something else";
}
,如果你的「如果」或「其他」裏面運行功能或語句失敗,至少你知道在它的基礎上其結果之一,而使用error_reporting可能會告訴你爲什麼失敗。
謝謝,這是最好的答案 – DomingoSL 2010-04-24 02:51:40