Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/286.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
PHP函数的检查和错误_Php - Fatal编程技术网

PHP函数的检查和错误

PHP函数的检查和错误,php,Php,有一种方法可以在php中检查函数是否失败时使用“IF” 前 谢谢 看一看。不过,你必须在你的函数中加入它们 编辑:顺便说一句,如果您的函数不应该返回布尔值,那么如果出现问题,您可以让它返回false,并检查如下: $result = getimagesize($image); if ($result === false) { // } 看一看。不过,你必须在你的函数中加入它们 编辑:顺便说一句,如果您的函数不应该返回布尔值,那么如果出现问题,您可以让它返回false,并检查如下: $

有一种方法可以在php中检查函数是否失败时使用“IF”

谢谢

看一看。不过,你必须在你的函数中加入它们

编辑:顺便说一句,如果您的函数不应该返回布尔值,那么如果出现问题,您可以让它返回false,并检查如下:

$result = getimagesize($image);

if ($result === false)
{
    //
}
看一看。不过,你必须在你的函数中加入它们

编辑:顺便说一句,如果您的函数不应该返回布尔值,那么如果出现问题,您可以让它返回false,并检查如下:

$result = getimagesize($image);

if ($result === false)
{
    //
}

无论“if”语句的条件是什么(括号内的内容),都将返回“true”或“false”。 如果条件返回“true”,将执行第一条语句,否则将执行第二条语句

你可以把这个,错误报告(全部);在脚本的最顶端,在打开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";
}
// 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
}

无论“if”语句的条件是什么(括号内的内容),都将返回“true”或“false”。
如果条件返回“true”,将执行第一条语句,否则将执行第二条语句

你可以把这个,错误报告(全部);在脚本的最顶端,在打开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";
}
// 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
}

我假设您对像
getimagesize
这样的函数特别感兴趣,它不会返回任何错误代码,这对我们来说很困难。但并非不可能

getimagesize
的文档说明:

如果无法访问文件名图像,或者它不是有效的图片,
getimagesize()
将生成级别为
E\u警告的错误。读取错误时,
getimagesize()
将生成级别为
E\u NOTICE
的错误

因此,您需要做两件事:

  • 得到错误的通知并以某种方式采取行动
  • 不要显示错误或以其他方式影响程序的执行(毕竟,您将自己处理错误处理代码中的任何错误)
  • 您可以使用和实现第一个。您可以使用
    @
    实现第二个

    因此,代码必须是这样的:

    <?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";
    }
    
    // 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\u,这不是一个好的做法)。因此,对于一个不仅有效而且设计精良的解决方案,您可以将所有这些都打包到一个类中。该类将定义:

  • 实现错误处理程序的方法(
    my\u error\u handler
    ,在上面的示例中)。要将对象方法设置为错误处理程序而不是全局函数,需要使用合适的第一个参数调用
    set\u error\u handler
    ;看
  • 该方法允许类设置错误处理程序,执行您选择的一些代码,保存“执行代码时出错”标志,并还原错误处理程序。例如,此方法可以获取调用代码提供的
    回调
    ,以及一组参数,并用于执行它。如果在执行过程中调用了上面#1中的错误处理程序集,请在对象的变量中标记该错误处理程序集。您的方法将
    call\u user\u func\u array
    的返回值返回给调用代码
  • 调用代码可用于访问上述#2结果的方法或变量
  • 因此,如果该类被称为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.:-)

    我假设您对像
    getimagesize
    这样的函数特别感兴趣,它不会返回任何错误代码,这对我们来说很困难。但并非不可能

    getimagesize
    的文档说明:

    如果无法访问文件名图像,或者它不是有效的图片,
    getimagesize()
    将生成级别为
    E\u警告的错误。读取错误时,
    getimagesize()
    将生成级别为
    E\u NOTICE
    的错误

    因此,您需要做两件事:

  • 得到错误的通知并以某种方式采取行动
  • 不要显示错误或以其他方式影响程序的执行(毕竟,您将自己处理错误处理代码中的任何错误)
  • 您可以使用和实现第一个。您可以使用
    @
    实现第二个

    因此,代码必须是这样的:

    <?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";
    }
    
    // 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\u,这不是一个好的做法)。因此,对于一个不仅有效而且设计精良的解决方案,您可以将所有这些都打包到一个类中。该类将定义:

  • 实现错误处理程序的方法(
    my\u error\u handler
    ,在上面的示例中)。要将对象方法设置为错误处理程序而不是全局函数,需要使用合适的第一个参数调用
    set\u error\u handler
    ;看
  • 该方法允许类设置错误处理程序,执行您选择的一些代码,保存“执行代码时出错”标志,并还原错误处理程序。例如,此方法可以获取调用代码提供的
    回调
    ,以及一组参数,并用于执行它。如果在执行过程中调用了上面#1中的错误处理程序集,请在对象的变量中标记该错误处理程序集。您的方法将
    call\u user\u func\u array
    的返回值返回给调用代码
  • 一种方法或变量