0

I am having a hard time finding out how to get a function to return a variable that I can use outside of the function, I have tried

return $response 

and I have tried

   return $response = $anothervariable

But the only way I have been able to get it to work is to echo the response and just put the html in there, I know there is a better way to do this, the problem is I can't figure it out. Any help would be appreciated!

function OpenCNAM($ph) {

$opencnamSID = '*****';
$opencnamTOKEN = '*****';
$query = "https://api.opencnam.com/v2/phone/".$ph."?format=text";
if(isset($opencnamSID)) { $query = "https://".$opencnamSID.":".$opencnamTOKEN."@api.opencnam.com/v2/phone/".$ph."?format=text"; }
$ch = curl_init();
curl_setopt ($ch, CURLOPT_URL, $query);
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, 2);   
curl_setopt ($ch, CURLOPT_TIMEOUT, 3);      
$response = curl_exec($ch);
curl_close($ch);

if($response != "") {    
  echo '<span title="'.$response.'"> 888-452-1505</span>';

} else {
    echo 'Nada';
}

}

OpenCNAM('8884521505');
2
  • What exactly are you trying to accomplish with this? Your first attempt is correct... Commented Feb 3, 2014 at 6:58
  • Try var_dump($response) Commented Feb 3, 2014 at 7:01

5 Answers 5

2
return $response;

is correct statement. It will return $response variable if program flow reaches at that point.

You can also pass parameters to function by reference like &$any_variable to set it with the desire value inside the function and play with it outside the function, after the function call.

Sign up to request clarification or add additional context in comments.

Comments

1

Remodify your code like this.

function OpenCNAM($ph) {

$opencnamSID = '*****';
$opencnamTOKEN = '*****';
$query = "https://api.opencnam.com/v2/phone/".$ph."?format=text";
if(isset($opencnamSID)) { $query = "https://".$opencnamSID.":".$opencnamTOKEN."@api.opencnam.com/v2/phone/".$ph."?format=text"; }
$ch = curl_init();
curl_setopt ($ch, CURLOPT_URL, $query);
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, 2);   
curl_setopt ($ch, CURLOPT_TIMEOUT, 3);      
$response = curl_exec($ch);
curl_close($ch);
$str="";
if($response != "") {    
  $str= '<span title="'.$response.'"> 888-452-1505</span>';

} else {
   $str= 'Nada';
}
return $str; //<---------- We are returning here
}

echo OpenCNAM('8884521505'); //<--- If you want to output.. or just $response = OpenCNAM('43535'); if you want to store it in a variable.

Comments

1

Just return the texts then you can do like this

function OpenCNAM($ph) {
    //...
    //...
    if($response != "") {    
        return '<span title="'.$response.'"> 888-452-1505</span>';

    } else {
        return 'Nada';
    }

}

$foo = OpenCNAM('8884521505');

Then, you can do what you want with $foo

Comments

0

try this

function OpenCNAM($ph)
 {

    $opencnamSID = '*****';
    $opencnamTOKEN = '*****';
    $query = "https://api.opencnam.com/v2/phone/".$ph."?format=text";
    if(isset($opencnamSID)) { $query = "https://".$opencnamSID.":".$opencnamTOKEN."@api.opencnam.com/v2/phone/".$ph."?format=text"; }
    $ch = curl_init();
    curl_setopt ($ch, CURLOPT_URL, $query);
    curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, 2);   
    curl_setopt ($ch, CURLOPT_TIMEOUT, 3);      
    $response = curl_exec($ch);
    curl_close($ch);

    if($response != "") 
    {    
        $res =  '<span title="'.$response.'"> 888-452-1505</span>';
    }
    else 
    {
        $res =  'Nada';
    }
return $res;
}

$return_res = OpenCNAM('8884521505');
echo $return_res;

Comments

0

If you aren't going to use the returned data anywhere, then echo will do just fine for you.

This was answered in PHP echo function return value vs echo inside function

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.