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 #MATLAB仿真#车辆换道路径规划
  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建
  • ¥15 数据可视化Python
  • ¥15 要给毕业设计添加扫码登录的功能!!有偿
  • ¥15 kafka 分区副本增加会导致消息丢失或者不可用吗?
  • ¥15 微信公众号自制会员卡没有收款渠道啊
  • ¥15 stable diffusion
  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条