Home  >  Article  >  Backend Development  >  Many problems occur after setting error handling in php set_error_handler? ?

Many problems occur after setting error handling in php set_error_handler? ?

WBOY
WBOYOriginal
2016-10-10 11:55:571053browse

The following is a custom method I customized to import variables in the array into global variables. However, when judging whether a variable with the same key name as the given array already exists in the global variable, an error is always reported. Even if @ is suppressed, an error will still be reported. Unless the set_error_handler is removed, the error will not be reported. How to solve this problem? ?

<code>ini_set('display_errors' , 'On');
error_reporting(E_ALL);

// 设置错误处理函数后, ini_set() && error_reporting() 这两个函数会失效,这是怎么回事(次要)??
set_error_handler('test');  
function test($err_level , $err_msg , $err_file , $err_line , $err_ctx){
    echo '发生错误了!';
    echo "\r\n";
    echo "\r\n";
}

function extract_global(array $arr = array()){
  if (empty($arr)) {
    return ;
  }

  foreach ($arr as $key => $val)
    { 
       // 这个地方怎么都抑制不了错误提示!
       // 如果把 set_error_handler 这个去掉,就可以抑制错误
       // 怎么解决这个问题(主要)??
       
       if (!is_null(@$GLOBALS[$key])) {
          trigger_error('已存在全局变量: ' . $key . '!' , E_USER_ERROR);
          exit;
       }
       
       $GLOBALS[$key] = $val;
    }
}

$arr = array(
  'name' => 'programmer' , 
  'hobby' => 'play computer game'
);

extract_global($arr);

print_r($name);
print_r("\r\n");
print_r($hobby);


</code>

Reply content:

The following is a custom method I customized to import variables in the array into global variables. However, when judging whether a variable with the same key name as the given array already exists in the global variable, an error is always reported. Even if @ is suppressed, an error will still be reported. Unless the set_error_handler is removed, the error will not be reported. How to solve this problem? ?

<code>ini_set('display_errors' , 'On');
error_reporting(E_ALL);

// 设置错误处理函数后, ini_set() && error_reporting() 这两个函数会失效,这是怎么回事(次要)??
set_error_handler('test');  
function test($err_level , $err_msg , $err_file , $err_line , $err_ctx){
    echo '发生错误了!';
    echo "\r\n";
    echo "\r\n";
}

function extract_global(array $arr = array()){
  if (empty($arr)) {
    return ;
  }

  foreach ($arr as $key => $val)
    { 
       // 这个地方怎么都抑制不了错误提示!
       // 如果把 set_error_handler 这个去掉,就可以抑制错误
       // 怎么解决这个问题(主要)??
       
       if (!is_null(@$GLOBALS[$key])) {
          trigger_error('已存在全局变量: ' . $key . '!' , E_USER_ERROR);
          exit;
       }
       
       $GLOBALS[$key] = $val;
    }
}

$arr = array(
  'name' => 'programmer' , 
  'hobby' => 'play computer game'
);

extract_global($arr);

print_r($name);
print_r("\r\n");
print_r($hobby);


</code>

The error handler is used to collect errors. If you want not to handle certain errors, just skip them in the error handler. It should not be controlled by the error level.

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn