如何更正此非法字符串偏移? [英] How do I correct this Illegal String Offset?

查看:195
本文介绍了如何更正此非法字符串偏移?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我收到此错误第32行上的/home/mysite/public_html/wp-content/themes/evento/lib/php/extra.class.php中的警告:非法字符串偏移'type'"

I am receiving this error "Warning: Illegal string offset 'type' in /home/mysite/public_html/wp-content/themes/evento/lib/php/extra.class.php on line 32"

,我意识到文件中的这段代码是错误的,但是我在PHP中还不是很出色,我想知道是否有人可以帮助我重写这一部分来消除错误.谢谢! (错误从第32行开始,这是下面if语句的开始)

and I realized this section of code in the file is wrong, however I'm not that great in PHP yet and I am wondering if someone can help me re-write this section to eliminate the error. Thanks! (the error starts on line 32 which is the beginning of the if statement below)

这是代码:

/* new version */
    function get_attachment_struct( $inputs ){
        $attach = array();

    if( $inputs['type'] == 'attach' ){ 
            $name = $inputs['name'];
            $attach = array(
                0 => array(
                    'name' => $name,
                    'type' => 'text',
                    'label' =>  'Attachment URL',
                    'lvisible' => false,
                    'upload' => true,
                ),
                1 => array(
                    'name' => $name .'_id',
                    'type' => 'hidden',
                    'upload' => true
                ),
            );

            if( isset( $inputs[ 'classes' ] ) ){
                $attach[0]['classes'] = $inputs[ 'classes' ];
                $attach[1]['classes'] = $inputs[ 'classes' ] . '_id';
            }
        }
        return $attach;
    }

    /* new version */

推荐答案

if ($inputs['type'] == 'attach') {

该代码有效,但是它希望函数参数$inputs是一个数组.使用$inputs['type']时出现非法字符串偏移"警告,表示正在向函数传递字符串而不是数组. (然后,由于字符串偏移量是数字,因此'type'不适合.)

The code is valid, but it expects the function parameter $inputs to be an array. The "Illegal string offset" warning when using $inputs['type'] means that the function is being passed a string instead of an array. (And then since a string offset is a number, 'type' is not suitable.)

所以从理论上讲问题出在其他地方,代码的调用者没有提供正确的参数.

So in theory the problem lies elsewhere, with the caller of the code not providing a correct parameter.

但是,此警告消息是PHP 5.4的新增功能.如果发生这种情况,旧版本未发出警告.他们会默默地将'type'转换为0,然后尝试获取字符串的字符0(第一个字符).因此,如果该代码可以正常工作,那是因为滥用这样的字符串不会对PHP 5.3及更低版本产生任何抱怨. (升级后,许多旧的PHP代码都遇到了此问题.)

However, this warning message is new to PHP 5.4. Old versions didn't warn if this happened. They would silently convert 'type' to 0, then try to get character 0 (the first character) of the string. So if this code was supposed to work, that's because abusing a string like this didn't cause any complaints on PHP 5.3 and below. (A lot of old PHP code has experienced this problem after upgrading.)

您可能想通过检查调用代码来调试为什么被赋予字符串,并通过在函数中执行var_dump($inputs);来找出函数具有什么值.但是,如果您只是想关闭警告以使其表现得像PHP 5.3,请将行更改为:

You might want to debug why the function is being given a string by examining the calling code, and find out what value it has by doing a var_dump($inputs); in the function. But if you just want to shut the warning up to make it behave like PHP 5.3, change the line to:

if (is_array($inputs) && $inputs['type'] == 'attach') {

这篇关于如何更正此非法字符串偏移?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆