警告:php 文件中的非法字符串偏移“参数”

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

Warning: Illegal string offset 'parameter' in php file

php

提问by Hady Shaltout

This error appears in index.php file

此错误出现在 index.php 文件中

Warning: Illegal string offset 'id' Warning: Illegal string offset 'std'

警告:非法字符串偏移 'id' 警告:非法字符串偏移 'std'

<?php
    global $options;
    foreach ($options as $value) {
        if (get_settings( $value['id'] ) === FALSE) {
            $$value['id'] = $value['std']; 
        } else {    
            $$value['id'] = get_settings( $value['id'] ); 
        }
    }

this problem appears when trying load new theme in Wordpress...

尝试在 Wordpress 中加载新主题时出现此问题...

回答by hakre

For the following two things:

对于以下两件事:

$value['id']
$value['std']

the variable $valueis not an array but a string. And the square brackets then are substring access. Because the string is empty, you get the error message.

变量$value不是数组而是字符串。然后方括号是子串访问。由于字符串为空,您会收到错误消息。

Demo: http://codepad.org/UDMtuO2x

演示:http: //codepad.org/UDMtuO2x

回答by VolkerK

The [] binds stronger than the $$, i.e. php first evaluates $value['id']and then would use this value as the name/identifier for the variable variable.
Use curly braces to change the precedence.

[] 绑定比 $$ 强,即 php 首先评估$value['id'],然后将此值用作变量变量的名称/标识符。
使用花括号更改优先级。

<?php
$array = array('id'=>123);
$value = 'array';
echo ${$value}['id'];

prints 123.

打印123

回答by WatsMyName

Its hard to say unless we know the pattern that $optionshold. Try

除非我们知道其中的模式,否则很难说$options。尝试

$value->idinstead of $value["id"]

$value->id代替 $value["id"]