php错误报告未初始化的字符串偏移量:0

Elb*_*bek 11 php error-reporting

我正在做一些PHP的东西,而不是它是调试模式.所以我就是我们

error_reporting(E_ALL);
Run Code Online (Sandbox Code Playgroud)

但是当我尝试访问字符串的任何字符时,由于错误报告而导致错误.

$sentence = "Hello World"; 
$sentence[0]   //Uninitialized string offset: 0
Run Code Online (Sandbox Code Playgroud)

编辑:

public static function prepareSentence($sentence)
{
    $sentence = trim($sentence);
    if ($sentence[0] == '"')  //Uninitialized string offset: 0 
        $sentence = substr($sentence, 1, strlen($sentence));

    if ($sentence[strlen($sentence) - 1] == '"')
        $sentence = substr($sentence, 0, -1);

    if ($sentence[0] == '"' || $sentence[strlen($sentence) - 1] == '"')
        return self::prepareSentence($sentence);
    return $sentence;
}
Run Code Online (Sandbox Code Playgroud)

我该怎么做才能在开发模式下工作.我需要error_reporting(E_ALL);

提前致谢.

xda*_*azz 23

对于空字符串,你不能使用$sentence[0],这将导致你得到的通知.

您可以添加!empty($sentence)以检查它是否为空.