PHP function goes into an infinite loop under certain conditions

青春壹個敷衍的年華 提交于 2019-12-12 04:38:46

问题


I am developing a facebook app and I have some php functions. When I call one of them, the request is sent over and over and goes in an infinite loop. I have no clue why. My function is:

function writeJSON()
{
    if (!$user_id) {
        echo "User not logged in";
    }
   else
{
global $arrayForJSON,$user_id;
$arrayForJSON['a']='b';
var_dump($arrayForJSON);
}  
}

If I run it just like that, it will show

array (size=1) 'a' => string 'b' (length=1)

Which is correct. However if I run another function that adds more elements to the$arrayForJSON, it goes into the loop. The other function is:

function getLikes()
{
    global $facebook,$user_id,$arrayForJSON;
    $likes=NULL;
    if($user_id) {
          try {
            if(is_null($likes))
            $likes = idx($facebook->api('/me/likes'), 'data', array());
            if ($likes) {
                $arrayForJSON['likes']=$likes;
            } 
        }
        catch(FacebookApiException $e){
             echo error_log($e);
        }
        echo "done";
        var_dump($arrayForJSON);
    }
    else
        echo "Not working";

Please give a helping hand, I've been working on that for some time and I have no clue what should I do.


If I call writeJSON() before calling getLikes(), it works. If I call it afterwards, it goes into the loop. I obviously need to call it after calling getLikes, because I need that data to be written to the JSON file


回答1:


There is no place whatsoever where there could be any looping or recursion in both of your functions. Most likely your getLikes() function is being called repeatedly from somewhere else that's outside of the posted code.

There is a somewhat similar question here, that suggests that Facebook authorization might be at fault somehow. I'm including the link here because maybe there is some code you're not showing that may cause the same behavior. However, it couldn't be caused just by the code you have posted.



来源:https://stackoverflow.com/questions/16225700/php-function-goes-into-an-infinite-loop-under-certain-conditions

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!