如果某个函数在php中失败,有一种方法可以检查“IF”吗?
实施例
If (getimagesize($image) returns and error) { echo 'Error: function dont work'; } else { // do something }
谢谢!
答案 0 :(得分:5)
我假设您特别感兴趣的是像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。 : - )
答案 1 :(得分:0)
看看exceptions。你必须把它们放在你的功能中。
编辑:顺便说一句,如果你的函数不应该返回一个布尔值,如果出现问题你总是可以返回false并检查如下:
$result = getimagesize($image);
if ($result === false)
{
//
}
答案 2 :(得分:0)
无论条件是什么,“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";
}
如果您在“if”或“else”中运行的函数或语句失败,至少根据结果知道它是哪一个,而error_reporting可能会告诉您失败的原因。