值为空时发生致命错误

时间:2013-10-06 10:28:41

标签: php wordpress

当我认为我有一个数组时,我有一个致命的错误声称我有一个字符串:

Fatal error: [] operator not supported for strings in /Applications/MAMP/htdocs/tankards_wordpress/wp-content/themes/tankardsleague/functions.php on line 566

以及以下警告: Warning: in_array() expects parameter 2 to be array, string given in /Applications/MAMP/htdocs/tankards_wordpress/wp-content/themes/tankardsleague/functions.php on line 579

我猜我在哪里有语法问题?我以为我已经启动了变量,但也许我错过了一个?我会很感激一些经验丰富的眼睛看看。

function forum_subscribe_member_player()
{
    global $user_ID;
    $players= get_users();

    foreach($players as $player)
    {
        $user_info = get_userdata($player->ID);
        $playeremail = $user_info->user_email;

        if(!empty($playeremail) && user_can( $player-> ID, 'contributor'))
        {                   
            $list = get_option('mf_forum_subscribers_1', array());


            if( is_player_subscribed($player->ID)) //remove player if already exists (user clicked unsubscribe)
            {
                $key = array_search($playeremail, $list);
                unset($list[$key]);
            }
            else
                $list[] = $playeremail;
            update_option('mf_forum_subscribers_1', $list);
        }
    }       
}

function is_player_subscribed($user_ID)
{
    if($user_ID)
      {
        $useremail = get_userdata($user_ID, 'user_email');

        $list = get_option("mf_forum_subscribers_1", array());
        if(!empty($list) && in_array($useremail, $list))
        {
          return true;
        }
        return false;
      }

}

function call_forum_subscribe_member_player()
{
    forum_subscribe_member_player();
} 

第566行$list[] = $playeremail;第579行为if(!empty($list) && in_array($useremail, $list))

1 个答案:

答案 0 :(得分:0)

好吧,您可以将$list类型转换为数组,以确保它始终是一个数组,即使初始值为null / empty / etc(基本上除数组之外的任何类型):

$list = (array)get_option('mf_forum_subscribers_1', array());