2014-04-01 84 views
0

我有以下情形变量范围在PHP

在查找功能我已经达到变量,我希望得到一些在$覆盖所有变量$跟随值。

下面的代码给出了follwer的最后一个值,而不是所有的总和。错误在哪里?

function x() 
{   
    lookup($tweetid,$connection);  
} 

function lookup($tweetid,$connection) 
{ 
    $tweets5 = $connection->get("https://api.twitter.com/1.1/statuses/retweets/".$tweetid.".json?count=1"); 
    $json = json_encode($tweets5); 
    foreach($tweets5 as $item) 
    { 
     $text = $item->text; 
     $user_id = $item->user->id; 
     $name = $item->user->name; 
     $follower = $item->user->followers_count; 

     $reach + = $follower; //This does not work 
     // $reach = $raech + $follower; //This does not work 
     $friend = $item->user->friends_count; 
     echo "Text : $text <br> ID : $user_id <br> Name : $name <br> Follower : $follower <br> Friends : $friend <br> ---"; 
    } 
    echo "<br> RT reach : $reach"; //This does not give some of folloewer count 
} 
+0

什么你得到$ follower变量 – Sadikhasan

回答

1

试图声明变量$范围= 0之前foreach

+0

这也没有帮助,仍然是相同的问题 –

+0

你在$ follower变量 – Sadikhasan

+0

中得到12,14这样的整数值。看到这里的参数https://dev.twitter.com/docs/api/1.1/get/statuses/retweets/%3Aid –

0

您需要声明foreach循环第一外$reach可变

$reach = 0; 
foreach($tweets5 as $item) 
    { 
     $text = $item->text; 
.......... 
... 
+0

这也没有帮助,仍然是同一个问题 –

+0

@Programming_crazy它应该工作...好吧。使用下面的一个。 $ reach = $ reach + $ follower; –

-1

只是声明$reach=0外foreach循环