php 如何更正此非法字符串偏移量?

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

How do I correct this Illegal String Offset?

phpwordpress

提问by gabearnold

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"

我收到此错误“警告:第 32 行的 /home/mysite/public_html/wp-content/themes/evento/lib/php/extra.class.php 中的非法字符串偏移‘类型’”

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)

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

Here is the code:

这是代码:

/* 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 */

回答by Boann

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

The code is valid, but it expects the function parameter $inputsto 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.)

代码是有效的,但它期望函数参数$inputs是一个数组。使用时的“非法字符串偏移”警告$inputs['type']意味着该函数正在传递一个字符串而不是数组。(然后因为字符串偏移量是一个数字,'type'所以不合适。)

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

所以理论上问题出在别处,代码的调用者没有提供正确的参数。

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.)

但是,此警告消息是 PHP 5.4 的新消息。如果发生这种情况,旧版本没有警告。他们会默默地转换'type'0,然后尝试获取字符串的字符 0(第一个字符)。因此,如果这段代码应该可以工作,那是因为滥用这样的字符串不会导致对 PHP 5.3 及以下版本的任何抱怨。(很多老PHP代码升级后都遇到过这个问题。)

You might want to debug whythe 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:

您可能希望通过检查调用代码来调试为什么给函数提供一个字符串,并通过var_dump($inputs);在函数中执行 a来找出它的值。但是,如果您只想关闭警告以使其表现得像 PHP 5.3,请将行更改为:

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

回答by user3015356

if(isset($rule["type"]) && ($rule["type"] == "radio") || ($rule["type"] == "checkbox") )
{
    if(!isset($data[$field]))
        $data[$field]="";
}

回答by Olf

I get the same error in WP when I use php ver 7.1.6 - just take your php version back to 7.0.20 and the error will disappear.

当我使用 php ver 7.1.6 时,我在 WP 中遇到相同的错误 - 只需将您的 php 版本恢复到 7.0.20,错误就会消失。