未定义的数组变量

时间:2014-04-26 08:34:19

标签: php mysql arrays global

$attacks_list = array();

function f1()
{     
    $sql = "SELECT * FROM user_pokemon_db WHERE user_id = '".$id."' AND pkmn_id = '".$pkmn_id."' " or die(mysql_error());
    $res = mysql_query($sql);
    $$attacks_list = mysql_result($res,0,"attacks");
    $attacks_list = unserialize($attacks_list);
    print_r($attacks_list);
 }    

我已经全局声明了这个数组..但是它仍然无法识别它! 有没有其他方法可以全局声明它?

1 个答案:

答案 0 :(得分:0)

如果您希望在函数中使用全局函数,则必须将其声明为函数级别的全局函数,例如

$attacks_list = array();

function f1()
{     
    global $attacks_list; // <-- ADDED
    $sql = "SELECT * FROM user_pokemon_db WHERE user_id = '".$id."' AND pkmn_id = '".$pkmn_id."' " or die(mysql_error());
    $res = mysql_query($sql);
    $$attacks_list = mysql_result($res,0,"attacks");
    $attacks_list = unserialize($attacks_list);
    print_r($attacks_list);
 }   

有关更多信息,请查看关于变量范围的php.net文章:http://www.php.net/manual/en/language.variables.scope.php