【问题标题】:How can i solve this error: llegal string offset 'text'?我该如何解决这个错误:非法字符串偏移“文本”?
【发布时间】:2014-01-25 15:44:40
【问题描述】:

我有这个代码:

if(strlen($userdata->yim['text']) > 2 && !isset($_POST['step1']) ){

    $GLOBALS['error']       = 1;
    $GLOBALS['error_type']  = "tip";
    $GLOBALS['error_msg']   = $userdata->yim['text'];}

我在此站点上阅读了有关此错误的信息,但我不知道如何将修复程序应用于我的特定代码。如果我重新发布问题,我很抱歉。

【问题讨论】:

  • 感谢您的及时答复,但正如我所说,我不知道该怎么做。我的开发人员给我留下了一些需要排除故障的错误,我不知道自己在做什么。
  • 是哪一行?是if 语句还是最后一行?
  • $userdata->yim 是一个 class->function() 调用吗?如果是这样,您的 PHP 版本可能不支持该语法,或者您可能需要改用 $userdata->yim()['text']
  • monkeyzeus,我很欣赏你的回答,但我不是程序员。你介意为我的水平降低答案吗?我在我的编辑器中尝试了 if(strlen($userdata->yim()['text']) > 2 && !isset($_POST['step1']) ){ 但它说语法错误。打扰了,我深表歉意。
  • 我会发布一个答案,因为它会更容易理解。顺便说一句,既然你是新人,你可以通过@MonkeyZeus 来引起我的注意,否则我不知道你发表了评论,除非我在几分钟后碰巧检查了你的问题 =)

标签: php string text offset


【解决方案1】:

尝试执行:var_dump($userdata->yim); 以验证 yim 是否确实存在并包含密钥“文本”。

甚至只是var_dump($userdata);

【讨论】:

  • 就在您执行if 测试的行之前,尝试执行 var_dump,然后立即执行die;,以便将 $userdata 的内容输出到屏幕进行调试。
  • 试过了,没变。同样的结果。我可能做错了吗?
【解决方案2】:

$userdata->yim['text'] 没有设置,所以你应该先检查一下:

if(isset($userdata->yim['text']) && strlen($userdata->yim['text']) > 2 && !isset($_POST['step1']) ){

但是,如果该部分代码依赖于该值,则在此之前会出现问题,这只会隐藏该问题。

【讨论】:

  • 这行得通。它确实隐藏了错误,但正如您预测的那样,问题仍然存在。
【解决方案3】:

为了调试它,你需要使用print_r() 以便你可以看到你的对象或数组的内容:

// see what $userdata contains - update your question with the results of the line below
echo '<div style="background-color:white; padding:15px;"><pre>'.print_r($userdata, true).'</pre></div>';

// see what $userdata->yim contains - update your question with the results of the line below as well
echo '<div style="background-color:white; padding:15px;"><pre>'.print_r($userdata->yim, true).'</pre></div>';

if(strlen($userdata->yim['text']) > 2 && !isset($_POST['step1']))
{
    $GLOBALS['error']       = 1;
    $GLOBALS['error_type']  = "tip";
    $GLOBALS['error_msg']   = $userdata->yim['text'];
}

【讨论】:

    猜你喜欢
    • 2018-09-16
    • 2023-03-18
    • 1970-01-01
    • 1970-01-01
    • 2022-01-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-07-21
    相关资源
    最近更新 更多