title: Print variable value out of foreach loop in php without page refreshing
body: <p>I am using curl php get result in return variable.i am using for each loop for retrieving all  key value in return variable and print i got all value through indexing .here all is fine but i'm trying to 6th no value store in $_SESSION['date'] variable and echo it outside the function .first time when i run page it will not print $_SESSION['date'].but when we refresh the page it will print.please give me solution.thank you my code is
  i want to print date in div without page refresh.</p>
  
  <pre><code>&lt;html&gt;
  &lt;body&gt;
    &lt;div id="head"&gt; &lt;?php echo $_SESSION['date']; ?&gt;&lt;/div&gt;
    &lt;?php
    function curlUsingGet($url, $data)
    {
    if(empty($url) OR empty($data))
    {
      return 'Error: invalid Url or Data';
    }
  
    $fields_string = '';
    foreach($data as $key=&gt;$value) { $fields_string .= $key.'='.urlencode($value).'&amp;'; }
    rtrim($fields_string,'&amp;');
  
    $urlStringData = $url.'?'.$fields_string;
    $_SESSION['urlStringData']=$urlStringData;
    $cookie= "cookie.txt";
    $ch = curl_init();
  
     curl_setopt($ch, CURLOPT_HEADER, 0);
     curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
     curl_setopt($ch, CURLOPT_CONNECTTIMEOUT,0);
     curl_setopt($ch, CURLOPT_USERAGENT , "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1)");
     curl_setopt($ch, CURLOPT_COOKIESESSION,true);
     curl_setopt ($ch, CURLOPT_COOKIEJAR, $cookie); 
     curl_setopt ($ch, CURLOPT_COOKIEFILE, $cookie);
     curl_setopt($ch, CURLOPT_FRESH_CONNECT,true);
     curl_setopt($ch, CURLOPT_URL,$urlStringData);
  
     $return = curl_exec($ch);
     $arrel = explode('|',$return);
  
  foreach($arrel as $val)
      {
      $new_el=explode('~',$val);
      print"&lt;ul data-role=listview  data-icon=false style=margin-top:3%;&gt;";
      print" &lt;li &gt;";
      print"&lt;a href='causelistmsg.php?a=$new_el[3]&amp;b=$new_el[4]'&gt;";
      print"&lt;font class=alignleft size='3' style=color:#FFFF00;&gt;";
      echo $new_el[0]; 
      print"&lt;/font&gt;";
      print"&lt;font size='3' class=aligncenter style=color:#FF8000;&gt;";
      echo $new_el[2];
      print"&lt;/font&gt;";
      print"&lt;font class=alignright size='3' style=color:#00CC00;&gt;";
      echo $new_el[1]; 
      print"&lt;/font&gt;";
      print"&lt;/a&gt;";
      print" &lt;/li &gt;";
      print" &lt;/ul&gt;";
      $_SESSION['date'] = $new_el[6];
      curl_close($ch);
      }
  
      $_SESSION['date'];
      $data = array('u' =&gt;$_SESSION['councel']);
      echo curlUsingGet('http://"url"/causelist/CauseListData',$data);
   ?&gt;
  </code></pre>