我收到此错误“警告:第 32 行 /home/mysite/public_html/wp-content/themes/evento/lib/php/extra.class.php 中的非法字符串偏移‘type’”
我意识到文件中的这部分代码是错误的,但是我对 PHP 还不太了解,我想知道是否有人可以帮助我重新编写这部分以消除错误。谢谢! (错误从第 32 行开始,即下面 if 语句的开头)
这是代码:
/* 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'
不适合。)
因此理论上问题出在其他地方,代码的调用者没有提供正确的参数。
然而,此警告消息是 PHP 5.4 中新增的。如果发生这种情况,旧版本不会发出警告。他们会默默地皈依'type'
to 0
,然后尝试获取字符串的第 0 个字符(第一个字符)。因此,如果这段代码应该有效,那是因为滥用这样的字符串不会在 PHP 5.3 及更低版本上引起任何投诉。 (很多旧的PHP代码在升级后都遇到过这个问题。)
您可能想要调试why通过检查调用代码为该函数提供一个字符串,并通过执行以下操作找出它的值var_dump($inputs);
在函数中。但如果您只是想关闭警告以使其表现得像 PHP 5.3,请将行更改为:
if (is_array($inputs) && $inputs['type'] == 'attach') {
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)