php 如何捕获此错误:“注意:未定义偏移量:0”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5373780/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 21:12:34  来源:igfitidea点击:

How to catch this error: "Notice: Undefined offset: 0"

phperror-handlingtry-catch

提问by meotimdihia

I want to catch this error:

我想抓住这个错误:

$a[1] = 'jfksjfks';
try {
      $b = $a[0];
} catch (\Exception $e) {
      echo "jsdlkjflsjfkjl";
}

Edit:in fact, I got this error on the following line: $parse = $xml->children[0]->children[0]->toArray();

编辑:实际上,我在以下行中收到此错误: $parse = $xml->children[0]->children[0]->toArray();

回答by zerkms

You need to define your custom error handler like:

您需要定义您的自定义错误处理程序,如:

<?php

set_error_handler('exceptions_error_handler');

function exceptions_error_handler($severity, $message, $filename, $lineno) {
  if (error_reporting() == 0) {
    return;
  }
  if (error_reporting() & $severity) {
    throw new ErrorException($message, 0, $severity, $filename, $lineno);
  }
}

$a[1] = 'jfksjfks';
try {
      $b = $a[0];
} catch (Exception $e) {
      echo "jsdlkjflsjfkjl";
}

回答by Macmade

You can't with a try/catch block, as this is an error, not an exception.

你不能使用 try/catch 块,因为这是一个错误,而不是一个例外。

Always tries offsets before using them:

在使用它们之前总是尝试偏移:

if( isset( $a[ 0 ] ) { $b = $a[ 0 ]; }

回答by ggderas

I know it's 2016 but in case someone gets to this post.

我知道现在是 2016 年,但万一有人看到这篇文章。

You could use the array_key_exists($index, $array)method in order to avoid the exception to happen.

您可以使用该array_key_exists($index, $array)方法以避免发生异常。

$index = 99999;
$array = [1,2,3,4,5,6];

if(!array_key_exists($index, $array))
{
    //Throw myCustomException;
}

回答by Prisoner

$a[1] = 'jfksjfks';
try {
  $offset = 0;
  if(isset($a[$offset]))
    $b = $a[$offset];
  else
    throw new Exception("Notice: Undefined offset: ".$offset);
} catch (Exception $e) {
  echo $e->getMessage();
}

Or, without the inefficiency of creating a very temporary exception:

或者,没有创建一个非常临时的异常的低效率:

$a[1] = 'jfksjfks';
$offset = 0;
if(isset($a[$offset]))
  $b = $a[$offset];
else
  echo "Notice: Undefined offset: ".$offset;

回答by MAChitgarha

Normally, you can't catch notices with try-catch block. But you can convert notices to exceptions! Use this way:

通常,您无法使用 try-catch 块捕捉通知。但是您可以将通知转换为异常!使用这种方式:

function get_notice($output)
{
    if (($noticeStartPoint = strpos($output, "<b>Notice</b>:")) !== false) {
        $position = $noticeStartPoint;
        for ($i = 0; $i < 3; $i++)
            $position = strpos($output, "</b>", $position) + 1;
        $noticeEndPoint = $position;
        $noticeLength = $noticeEndPoint + 3 - $noticeStartPoint;
        $noticeMessage = substr($output, $noticeStartPoint, $noticeLength);
        throw new \Exception($noticeMessage);
    } else
        echo $output;
}

try {
    ob_start();
    // Codes here
    $codeOutput = ob_get_clean();
    get_notice($codeOutput);
} catch (\Exception $exception) {
    // Catch (notice also)!
}

Also, you can use this function for to catch warnings. Just change function name to get_warning and change "<b>Notice</b>:"to "<b>Warning</b>:".

此外,您可以使用此功能来捕捉警告。只需将函数名称更改为 get_warning 并更改"<b>Notice</b>:""<b>Warning</b>:".

Note:The function will catch an innocent output that contains:

注意:该函数将捕获包含以下内容的无害输出:

<b>Notice</b>:

<b>注意</b>:

But to escape from this problem, simply, change it to:

但是为了摆脱这个问题,简单地把它改成:

<b>Notice:</b>

<b>注意:</b>

回答by carlos

Im sure why the Error Throw but i fix some..

我确定错误抛出的原因,但我修复了一些..

in html2pdf.class.php

在 html2pdf.class.php

on Lines 2132:

在第 2132 行:

//FIX:
$ctop=$corr[$y][$x][2]<=count($sw)?$corr[$y][$x][2]:count($sw);
$s = 0; for ($i=0; $i<$ctop; $i++) {$s+= array_key_exists($x+$i, $sw)? $sw[$x+$i]:0;}

SAME On line 2138:

在线 2138 相同:

//FIX:

$ctop=$corr[$y][$x][2]<=count($sw)?$corr[$y][$x][2]:count($sw);
 for ($i=0; $i<$ctop; $i++) {

the problem the array $sw not have a key of $corr[$y][$x][2] so i fix the loop for to max count($sw) to fix .. I dont know if that create an another consecuense but i resolve my problem y dont have any more errors..

问题数组 $sw 没有 $corr[$y][$x][2] 的键,所以我修复了最大计数($sw)的循环来修复..我不知道这是否会造成另一个后果但我解决了我的问题你没有更多的错误..

So i hope works to you ..!!! Beats Reguards

所以我希望对你有用..!!! Beats Reguards