【发布时间】:2010-04-24 00:46:43
【问题描述】:
如果某个函数在 php 中失败,是否可以使用 "IF" 进行检查?
例如
If (getimagesize($image) returns and error) { echo 'Error: function dont work'; } else { // do something }
谢谢!
【问题讨论】:
标签: php
如果某个函数在 php 中失败,是否可以使用 "IF" 进行检查?
例如
If (getimagesize($image) returns and error) { echo 'Error: function dont work'; } else { // do something }
谢谢!
【问题讨论】:
标签: php
我假设您对 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";
}
如果您在“if”或“else”中运行的函数或语句失败,至少您可以根据结果知道是哪一个,error_reporting 可能会告诉您失败的原因。
【讨论】: