检查PHP函数和错误

时间:2010-04-24 00:46:44

标签: php

如果某个函数在php中失败,有一种方法可以检查“IF”吗?

实施例

If (getimagesize($image) returns and error)  {
echo 'Error: function dont work';
}
else
{
// do something
}

谢谢!

3 个答案:

答案 0 :(得分:5)

我假设您特别感兴趣的是像getimagesize这样的函数,它不会返回任何错误代码并使我们难以理解。但并非不可能。

getimagesize州的文档:

  

如果无法访问文件名图片,或者如果图片不是有效图片,getimagesize()将生成级别为E_WARNING的错误。在读取错误时,getimagesize()将生成级别E_NOTICE的错误。

因此,您需要做两件事:

  1. 收到错误通知并以某种方式对其采取行动
  2. 根本没有显示错误或以其他方式影响程序的执行(毕竟,您将自己处理错误处理代码中的任何错误)
  3. 您可以使用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,这不是一个好习惯)。因此,对于不仅可以工作而且设计精良的解决方案,您可以将所有这些打包在一个类中。该课程将定义:

    1. 实现错误处理程序的方法(上例中的my_error_handler)。要将对象方法设置为错误处理程序而不是全局函数,您需要使用合适的第一个参数调用set_error_handler;见the documentation for callback
    2. 一种方法,它允许类设置错误处理程序,执行您选择的某些代码,保存“执行代码时发生的错误”标志,并恢复错误处理程序。这种方法可以例如获取调用代码和参数数组提供的callback并使用call_user_func_array执行它。如果在执行期间调用上面#1中设置的错误处理程序,请在对象的变量中标记它。您的方法会将call_user_func_array的返回值返回给调用代码。
    3. 调用代码可用于访问上述#2的结果的方法或变量。
    4. 那么,如果该类被称为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可能会告诉您失败的原因。