【问题标题】:php error reporting Uninitialized string offset: 0php报错 Uninitialized string offset: 0
【发布时间】:2012-02-26 15:54:10
【问题描述】:

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

error_reporting(E_ALL);

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

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

编辑:

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

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

提前致谢。

【问题讨论】:

  • 多一点代码会更好。你以前用句子变量吗?
  • 没问题。我刚刚编辑过。
  • 运行良好:ideone.com/R6y3n
  • 这不应该返回任何错误。您能否提供至少与本节相关的完整代码。据我所知,这很好
  • 当我回显 $sentence 时,它​​会给出正确的字符串。

标签: php error-reporting


【解决方案1】:

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

【讨论】:

    【解决方案2】:

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

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

    【讨论】:

    • 实际上我正在从文件中解析一堆字符串。所以文件不是空的。有很多字符串块,但由于该方法的一些空格出现空字符串并给出错误。这是我的错。我只是检查并意识到。发送。
    • empty的问题是empty("0")也会返回true。
    • @KeVin 以编程方式 0 和空是不同的,空是没有数据,而零是有效值。
    猜你喜欢
    • 2017-01-14
    • 2010-09-25
    • 2023-01-31
    • 1970-01-01
    • 1970-01-01
    • 2016-11-12
    • 1970-01-01
    • 1970-01-01
    • 2020-12-08
    相关资源
    最近更新 更多