PHP函数在某些情况下会陷入无限循环 [英] PHP function goes into an infinite loop under certain conditions

查看:467
本文介绍了PHP函数在某些情况下会陷入无限循环的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个Facebook应用程序,并且我有一些php函数.当我调用其中之一时,请求一遍又一遍地发送并进入无限循环.我不知道为什么. 我的功能是:

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'=>字符串'b'(length = 1)

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

这是正确的.但是,如果我运行另一个向$arrayForJSON添加更多元素的函数,它将进入循环.另一个功能是:

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.

如果在致电getLikes()之前先致电writeJSON(),它会起作用.如果以后再调用它,它将进入循环.很显然,在调用getLikes之后,我需要调用它,因为我需要将该数据写入JSON文件

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

推荐答案

在两个函数中都没有任何地方可能存在任何循环或递归.您的getLikes()函数很可能是从已发布代码之外的其他地方重复调用的.

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.

此处,存在一个类似的问题,表明Facebook授权可能会以某种方式出故障.我在此处包括该链接,因为也许您未显示一些代码可能会导致相同的行为.但是,这不仅仅是由您发布的代码引起的.

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.

这篇关于PHP函数在某些情况下会陷入无限循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆