php 报错 Uninitialized string offset: 0

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9138675/
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-26 06:14:13  来源:igfitidea点击:

php error reporting Uninitialized string offset: 0

phperror-reporting

提问by Elbek

i am doing a stuff in php and not it is debug mode. So i am us

我正在用 php 做一些事情,而不是调试模式。所以我是我们

error_reporting(E_ALL);

but When i try to access any character of string it gives me error due to the error reporting.

但是当我尝试访问字符串的任何字符时,由于错误报告,它给了我错误。

$sentence = "Hello World"; 
$sentence[0]   //Uninitialized string offset: 0

edited:

编辑:

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;
}

How should I do in order to work in dev mode. I need the error_reporting(E_ALL);

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

thanks in advance.

提前致谢。

回答by xdazz

For empty string, you can't use $sentence[0], that will cause the notice you got.

对于空字符串,您不能使用$sentence[0],这会导致您收到通知。

You can add !empty($sentence)to check if it is empty.

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

回答by Sean

You are creating $sentence as a string ($sentence = "Hello World";) and then calling it as an array ($sentence[0]). That is no longer allowed. It used to work silently in the background and change the variable to an array for you with that error but as of PHP 7.1 it will fail completely. Comes out as an E_NOTICE error (should actually be upgraded to E_DEPRECATED or something as it now fails but whatever).

您将 $sentence 创建为字符串 ($sentence = "Hello World";),然后将其作为数组调用 ($sentence[0])。这不再被允许。它曾经在后台静默工作,并在出现该错误时将变量更改为数组,但从 PHP 7.1 开始,它将完全失败。出现 E_NOTICE 错误(实际上应该升级到 E_DEPRECATED 或其他东西,因为它现在失败了,但无论如何)。