上下文:在我的WordPress插件安装的网站之一,我看到一系列的PHP警告,但我不完全确定为什么会发生这种情况。我希望这里有人能帮我弄清楚如何解决这个警告。如何解决这些非法字符串偏移警告?
代码示例:
function my_function($array) {
if (! isset($array['where'])) { $array['where'] = 'after'; }
if (! isset($array['echo'])) { $array['echo'] = false; }
if (! isset($array['content'])) { $array['content'] = false; }
$array['shortcode'] = true;
$array['devs'] = true;
return social_warfare($array);
}
add_shortcode('my_shortcode', 'my_function');
的警告:
警告:非法串在 /home/playitda/public_html/domain.com/wp-偏移 '其中' content/plugins/my_plugin/functions/frontend-output/shortcodes.php on line 14
警告:非法串在 /home/playitda/public_html/domain.com/wp-content/plugins/my_plugin/functions/frontend-output/shortcodes.php 抵销“回响”第15行
警告:不能一个空字符串赋值给一个字符串 /home/playitda/public_html/domain.com/wp-content/plugins/my_plugin/functions/frontend-output/shortcodes.php 抵消线15
警告:非法串抵消'content' /home/playitda/public_html/domain.com/wp-content/plugins/my_plugin/functions/frontend-output/shortcodes.php on line 16
警告:不能为空字符串赋值给一个串上线 /home/playitda/public_html/domain.com/wp-content/plugins/my_plugin/functions/frontend-output/shortcodes.php 偏移16
警告:非法串偏移 '短码' 在 /home/playitda/public_html/domain.com/wp-content/plugins/my_plugin/functions/frontend-output/shortcodes.php 第18行
警告:非法字符串偏移'devs' /home/playitda/public_html/domain.com/wp-content/plugins/my_plugin/functions/frontend-output/shortcodes.php on第19行
出于某种原因,每次遇到数组中的某个索引时都会发出警告。我该如何解决?谢谢!
它看起来像函数期待一个数组并获取一个字符串。 –
那么我该如何强制这是一个数组?我是否在参数中声明它,或者在继续之前检查它并使其成为一个数组? –
是的,你可以['输入提示'](http://php.net/manual/en/functions.arguments.php#functions.arguments.type-declaration)(远远超过提示7. +)函数签名或显式检查类型...'is_array'。虽然不想在期待数组时传递一个字符串。 – ficuscr