dpg76975 2017-09-14 18:22
浏览 393
已采纳

如何修复这些非法字符串偏移警告?

Context: On one of the sites where my WordPress plugin is installed I'm seeing a series of PHP warnings, but I'm not entirely sure why this is happening. I'm hoping someone here can help me figure out how to solve this warning.

Code Sample:

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' );

The Warning:

Warning: Illegal string offset 'where' in /home/playitda/public_html/domain.com/wp-content/plugins/my_plugin/functions/frontend-output/shortcodes.php on line 14

Warning: Illegal string offset 'echo' in /home/playitda/public_html/domain.com/wp-content/plugins/my_plugin/functions/frontend-output/shortcodes.php on line 15

Warning: Cannot assign an empty string to a string offset in /home/playitda/public_html/domain.com/wp-content/plugins/my_plugin/functions/frontend-output/shortcodes.php on line 15

Warning: Illegal string offset 'content' in /home/playitda/public_html/domain.com/wp-content/plugins/my_plugin/functions/frontend-output/shortcodes.php on line 16

Warning: Cannot assign an empty string to a string offset in /home/playitda/public_html/domain.com/wp-content/plugins/my_plugin/functions/frontend-output/shortcodes.php on line 16

Warning: Illegal string offset 'shortcode' in /home/playitda/public_html/domain.com/wp-content/plugins/my_plugin/functions/frontend-output/shortcodes.php on line 18

Warning: Illegal string offset 'devs' in /home/playitda/public_html/domain.com/wp-content/plugins/my_plugin/functions/frontend-output/shortcodes.php on line 19

For some reason, it's throwing a warning every time it encounters one of the indices in the array. How do I fix this? Thanks!

  • 写回答

2条回答 默认 最新

  • dpt1712 2017-09-14 18:32
    关注

    The usage of the function is_array() in the beginning of your function can give you the insurance, that, if someone pass you something else than an array, the variable is reinitialised as an empty array.

    Unsetting or nulling it before doing that is useless, because, as of PHP 5.3, PHP does have a garbage collector mechanism.

    /**
     * @params array $array
     */
    function my_function( $array ) {
       if ( ! is_array ( $array ) ) { $array = [] };
       /**
        * Or, if you don't like the short array notation:
        * if ( ! is_array ( $array ) ) { $array = 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' );
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 微信会员卡等级和折扣规则
  • ¥15 微信公众平台自制会员卡可以通过收款码收款码收款进行自动积分吗
  • ¥15 随身WiFi网络灯亮但是没有网络,如何解决?
  • ¥15 gdf格式的脑电数据如何处理matlab
  • ¥20 重新写的代码替换了之后运行hbuliderx就这样了
  • ¥100 监控抖音用户作品更新可以微信公众号提醒
  • ¥15 UE5 如何可以不渲染HDRIBackdrop背景
  • ¥70 2048小游戏毕设项目
  • ¥20 mysql架构,按照姓名分表
  • ¥15 MATLAB实现区间[a,b]上的Gauss-Legendre积分