0

Here is my script. I declared few variable outside function. I want to use it in function, would it be available?

<?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;
}

?>
  • 2
    Change your function definition as `function lookup($userid,$connection)` and don't use **global** keyword. Call that function like `lookup($item->user->id_str,$connection);` – Shankar Narayana Damodaran Mar 31 '14 at 07:35
  • @ShankarDamodaran is there any issue on using **global**? – Lepanto Mar 31 '14 at 07:42
  • @Lepanto: Yes, [the](http://stackoverflow.com/a/5166527/1438393)[re](http://c2.com/cgi/wiki?GlobalVariablesAreBad) [a](http://stackoverflow.com/questions/5283102/how-is-testing-registry-pattern-or-singleton-hard-in-php/5283151#5283151)[re](http://en.wikipedia.org/wiki/SOLID_%28object-oriented_design%29) – Amal Murali Mar 31 '14 at 07:46
  • @Lepanto if you use globals, errors in your code are so damn difficult to detect. The thread which Amal Murali linked in the comment above shows a really good example. Try to avoid global whenever possible. – Realitätsverlust Mar 31 '14 at 07:48
  • @YUNOWORK WORK I don't think there is any harm in using **global** as its still alive in PHP. It's usage dependence upon the application type, size – Lepanto Mar 31 '14 at 07:57
  • @Lepanto there is no harm, i didnt say that. I just said that using the `global` keyword makes errors difficult to find. For a small application, you can of course use it, even if there are always better options. Its a bit like the `go to` in C# - its there, but noone wants to use it. – Realitätsverlust Mar 31 '14 at 08:09

4 Answers4

2

A function has its own scope. You have to provide all variables you want to use in arguments except if they are in the $_SESSION, $_SERVER etc. variables.

Bowdzone
  • 3,827
  • 11
  • 39
  • 52
2

Hand it over via parameter:

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

Only Superglobals are availible inside functions. Everything else is handed over via parameters.

Realitätsverlust
  • 3,941
  • 2
  • 22
  • 46
-2

You can use outside variable by defining them as global or you can pass them to functions as a parameters.

$str= 'str';

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

or

function test($str)
{
    echo $str;
}
Anil R
  • 289
  • 1
  • 6
-3

using the global keyword should do the trick:

$foo = '123';

function bar() {
    global $foo;
    echo $foo;
}

but the way I see it, you could just pass the variable to that function instead.

Fadey
  • 430
  • 2
  • 11
  • agreed, but he asked to know how is it possible. – Fadey Mar 31 '14 at 07:44
  • 1
    Then you should explain it is not a good practice and list the possible alternatives. – Amal Murali Mar 31 '14 at 07:47
  • not a good practice does not mean it is not possible. btw, it is very common on javascript scripts. – Fadey Mar 31 '14 at 07:51
  • The fact that it's common in js (and in PHP) doesn't make it good practise – Mark Baker Mar 31 '14 at 08:02
  • I agree it's a bad practice, but the question was "How is it possible?" – Fadey Mar 31 '14 at 08:03
  • @Fadey You dont get the point. The question was: "How is it possible." and not "How is it possible using the global keyword?". Of course your approach works, but why dont you just show him the proper way if you even know that your way is a bad practice. Giving advices is fine, but recommending globals instead of parameters is like recommending mysql_* instead of PDO. – Realitätsverlust Mar 31 '14 at 08:12
  • you assume that the question author knows about the global keyword. I think he wanted to know if it's possible in php, and even though you dont like it, it is possible. – Fadey Mar 31 '14 at 08:17
  • No. The point is, you can provide an answer to the question, but add the fact that it is **not** good practice, and **list alternatives**. This would make it a "good answer". Same with anyone using `mysql_*` functions etc. – Jimbo Mar 31 '14 at 08:33
  • well, I did pointed out my personal view regarding using globals, you are just being niggling, trying to make a point which was clearly didn't matter to the author. I do think that using globals is in fact a bad practice. BUT, it is still a relative perspective. good or bad practice is not a matter of definition, but a more abstract perspective of us as a developers. – Fadey Mar 31 '14 at 10:37