Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/288.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
php中函数外部的变量作用域_Php - Fatal编程技术网

php中函数外部的变量作用域

php中函数外部的变量作用域,php,Php,我有以下几种情况 在查找函数中,我有一个reach变量,我想得到$reach变量中所有$follower值的一部分 下面的代码给出了follower的最后一个值,而不是所有值的总和。臭虫在哪里 function x() { lookup($tweetid,$connection); } function lookup($tweetid,$connection) { $tweets5 = $connection->get("https://api.t

我有以下几种情况

在查找函数中,我有一个reach变量,我想得到$reach变量中所有$follower值的一部分

下面的代码给出了follower的最后一个值,而不是所有值的总和。臭虫在哪里

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
}
函数x()
{        
查找($tweetid,$connection);
}
函数查找($tweetid,$connection)
{
$tweets5=$connection->get(“https://api.twitter.com/1.1/statuses/retweets/“$tweetid.”.json?count=1“;
$json=json_编码($tweets5);
foreach($tweets5作为$item)
{
$text=$item->text;
$user\u id=$item->user->id;
$name=$item->user->name;
$follower=$item->user->followers\u count;
$reach+=$follower;//这不起作用
//$reach=$raech+$follower;//这不起作用
$friend=$item->user->friends\u count;
echo“Text:$Text
ID:$user_ID
Name:$Name
Follower:$Follower
朋友:$friend
--”; } echo“
RT reach:$reach”//这并没有给出一些下面的计数 }
foreach
循环之前尝试声明变量$reach=0

您需要首先在
foreach
循环之外声明
$reach
变量

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

只需在foreach循环外部声明$reach=0您在$follower variable中得到的值这也没有帮助,仍然是相同的问题$follower variable12、14这样的整数值。请参见此处的参数,这也没有帮助,仍然相同issue@Programming_crazy它应该能用……好的。使用下面的一个$到达=$reach+$follower;