【问题标题】:variable scope in php functionphp函数中的变量范围
【发布时间】:2014-03-31 07:33:34
【问题描述】:

这是我的脚本。 我在函数外声明了几个变量。我想在函数中使用它,可以使用吗?

<?php

session_start();
require_once('twitteroauth/twitteroauth.php');
require_once('follow.php');

require_once('config.php');

if (empty($_SESSION['access_token']) || empty($_SESSION['access_token']['oauth_token']) || empty($_SESSION['access_token']['oauth_token_secret'])) {
header('Location: ./clearsessions.php');
}
$access_token = $_SESSION['access_token'];
$connection = new TwitterOAuth(CONSUMER_KEY, CONSUMER_SECRET, $access_token['oauth_token'], $access_token['oauth_token_secret']);
$content = $connection->get('account/verify_credentials');
$twitteruser = $content->{'screen_name'};
$userid = $content->{'id'};
$temp = "1";
$tweets1 = $connection->get("https://api.twitter.com/1.1/statuses/retweets_of_me.json?count=200");
$tweets3 = $connection->get("https://api.twitter.com/1.1/statuses/user_timeline.json?trim_user=true&include_rts=true");

$tweets4 = $connection->get("https://api.twitter.com/1.1/statuses/home_timeline.json?include_rts=true&trim_use=true");

foreach ($tweets1 as $item)
{
$text = $item->text;
$follow_count = getfollow($m[1]);
echo "followe count is $follow_count <br>";
lookup($item->user->id_str);

}

function lookup($userid)
{
//echo "userid : $userid temp : $temp";
$tweets5 = $connection->get("https://api.twitter.com/1.1/users/lookup.json?user_id='.$userid.' ");

$CONNECTION IS not availabl here? WHY?

foreach ($tweets5 as $item)
{
$text = $item->name;

}
return;
}

?>

【问题讨论】:

  • 把你的函数定义改成function lookup($userid,$connection),不要使用global关键字。调用该函数,如lookup($item-&gt;user-&gt;id_str,$connection);
  • @ShankarDamodaran 使用 global 有什么问题吗?
  • @Lepanto:是的,thereare
  • @Lepanto 如果您使用全局变量,代码中的错误非常难以检测。 Amal Murali 在上面的评论中链接的线程显示了一个非常好的例子。尽量避免全局。
  • @YUNOWORK WORK 我认为使用 global 并没有什么害处,因为它在 PHP 中仍然存在。它的使用依赖于应用程序类型、大小

标签: php


【解决方案1】:

函数有自己的作用域。您必须提供要在参数中使用的所有变量,除非它们位于 $_SESSION、$_SERVER 等变量中。

【讨论】:

    【解决方案2】:

    通过参数交出:

    function lookup($userid, $connection) {
        //code here
    }
    

    只有超全局变量在函数内部可用。其他一切都通过参数传递。

    【讨论】:

      【解决方案3】:

      您可以通过将外部变量定义为全局变量来使用它们,也可以将它们作为参数传递给函数。

      $str= 'str';
      
      function test()
      {
          global $str;
          echo $str;
      }
      

      function test($str)
      {
          echo $str;
      }
      

      【讨论】:

        【解决方案4】:

        使用 global 关键字应该可以解决问题:

        $foo = '123';
        
        function bar() {
            global $foo;
            echo $foo;
        }
        

        但在我看来,您可以将变量传递给该函数。

        【讨论】:

        • 同意了,但他想知道这怎么可能。
        • 那么你应该解释这不是一个好的做法并列出可能的替代方案。
        • 不是一个好的做法并不意味着不可能。顺便说一句,这在 javascript 脚本中很常见。
        • 它在 js(和 PHP)中很常见的事实并不能使它成为好的实践
        • 我同意这是一种不好的做法,但问题是“这怎么可能?”
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2016-10-26
        • 2013-10-28
        • 2014-11-10
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-12-30
        相关资源
        最近更新 更多