tags:

views:

46

answers:

3
<?php function curl($mail){

    $go = curl_init();
    $access_token = '1234567890|5fabcd37ef194fee-1752237355|JrG_CsXLkjhcQ_LeYPU.';
    curl_setopt($go, CURLOPT_URL,'https://graph.facebook.com/search?q='.$mail.'&amp;type=user&amp;access_token='.$access_token);
    curl_setopt($go, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US; rv:1.9.2.8) Gecko/20100722 Firefox/3.6.8");
    curl_setopt($go, CURLOPT_POST, 0);
    curl_setopt($go, CURLOPT_HEADER, 0);
    curl_setopt($go, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($go, CURLOPT_SSL_VERIFYPEER, false);
    $json = curl_exec($go);
    curl_close($go);

    $arr = json_decode($json,1);
    if(isset($arr['data']['0']['id'])) {
        return $arr['data']['0']['id'];
    } else {
        return false;
    }
} ?>

I'm placing $name = $arr['data']['0']['name']; right above the return $arr['data']['0']['id']; However I cannot echo the $name variable after I run $a = curl($mail);

+3  A: 

If you mean this:

function abc() {
    if(isset($arr['data']['0']['id'])) {
        $name = $arr['data']['0']['name'];
        return $arr['data']['0']['id'];
    }
    ...
}
echo $name;

It's not possible, unless you declare $name as global. The variable $name has local-scope and cannot be refered to outside the function unless it has been a global. There are other tricks (like reference-variable as function-parameter) too achieve your goal.

Edit for reference-example:

$refVar = 'foo';
function withRef(&$var) {
    echo $var; // returns 'foo'
    $var = 'bar';
    return 'return some other value';
}
$result = withRef($refVar);
echo $refVar; // now returns 'bar'
alopix
Beat me to it, well put. +1 - why not show an example of a reference variable if you're in the mood?
Pekka
i edited the answer for better format
alopix
+2  A: 

Of course not. A local variable stays local, it doesn't leak to the outside. Do you have the slightest idea how much havok that could cause if it worked? You could never rely on that some function's local variables don't shadow your own.

Return some data structure (e.g. array('name' => ..., 'id' => ...)) from which the caller can get the info he needs. Or use a reference parameter and set that one.

delnan
+1  A: 

Unless you're updating a global variable (the use of which isn't best practice) the only way to "access" a variable that's present within a function/method is if the function/method returns the value you require or accepts the variable by reference as a parameter and updates the variable.

i.e.:

// curl returns the required value.
$name = curl('email@xxx');

or

// The curl function optionally accepts the '$name' parameter
// which can be overwritten in its original scope.
function curl($email, &name=null) {
    ...
    $name = 'xxx';
    ...
}

if(curl('email@xxx', $name))
...

This is due to the fact that variables in functions/methods/classes, etc. are only visible within the scope they're defined with. (This is a good thing.)

You can read more about this at: http://php.net/manual/en/language.variables.scope.php

Incidentally, I'd be tempted not to name a function "curl", as this is a risky in terms of clashing with an existing function - something like "fetchUserData" might be a better approach.

middaparka