PHP foreach堆栈 - 在调用下一个迭代时,每个循环中调用的函数是否仍然可以运行

问题描述:

I am having problems with cURL not being able to connect to a server that returns an xml feed and am not sure if my code is stacking up and causing the problem. Is it possible the final function called in this foreach loop is still running when the next loop iteration comes round.

Is it possible to make sure all functions in the loop complete before the next iteration begins, or does foreach do this by default anyway? I tried setting a return true on process_xml() and running a test in the loop: if($this->process_xml($xml_array))continue; but it didn't seem to have an effect and seems like a bad idea anyway.

foreach($arrayOfUrls as $url){

    //retrieve xml from url as string.
    if($url_xml_string = $this->getFeedStringUsing_cURL($url)){
        $xml_object = simplexml_load_string($url_xml_string);
        $xml_array = $this->feedStringToArray($xml_object);
        //process the xml.
        $this->process_xml($xml_array);
    }                                 
}

我遇到问题,cURL无法连接到返回xml feed的服务器,我不确定是否 我的代码堆积起来并导致问题。 当下一个循环迭代到来时,这个foreach循环中调用的最终函数是否仍然可以运行。 p>

是否有可能确保循环中的所有函数在下一次迭代开始之前完成 ,或者foreach默认做到这一点吗? 我尝试在process_xml()上设置一个返回true并在循环中运行一个测试:if($ this-> process_xml($ xml_array))继续; 但它似乎没有效果,看起来好像一个坏主意 无论如何。 p>

  foreach($ arrayOfUrls as $ url){
 
 //从url中检索xml为字符串。
 if($ url_xml_string = $ this->  getFeedStringUsing_cURL($ url)){
 $ xml_object = simplexml_load_string($ url_xml_string); 
 $ xml_array = $ this-> feedStringToArray($ xml_object); 
 //处理xml。
 $ this-> process_xml  ($ xml_array); 
} 
} 
  code>  pre> 
  div>

No, this is not possible. Each statement is executed and finished before the next statement is run.

and am not sure if my code is stacking up

Not sure? If it's important to you why don't you find out? Without knowing what OS you are running on its rather hard to advise how you'd go about that - but netstat might be a good starting point.

Is it possible the final function called in this foreach loop is still running

It's highly improbable - PHP scripts run in a single thread of execution unless you tell them not to - but the curl extension allows you to define callbacks into your php code which run before the operation completes, and the curl_multi_ family of functions also allow you to run php code while requests are in progress.