gpt4 book ai didi

php - 如何在不编写新函数的情况下从另一个文件调用变量

转载 作者:行者123 更新时间:2023-12-01 21:59:06 24 4
gpt4 key购买 nike

如何从另一个文件调用变量?一个函数可以返回 5 个值吗?在我的 function.php 文件中,我获取了不同变量中的许多值。例如,让我们看一下下面的函数
Function.php 文件

function getID($url)
{
global $link;
$ch = curl_init("http://example.com/?id=".$url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
$raw = curl_exec($ch);
curl_close($ch);
$data = json_decode($raw);
$id=$data->id;
$cname=$data->name;
$info=$data->client_info;
$up=$data->voteup;
$cat=$data->category;

return $id;
}

index.php文件

  $myid=getID($url);
echo "My ID : " . $myid; -->This is working but not the below four....
echo "Client Name : "
echo "Information : "
echo "Up Votes : "
echo "Category : "

我不想将所有内容保存在一个文件中。在index.php文件中,我还想输出'myid'下的'cname','info','up','cat'值。我正在考虑制作4个不同的函数并将它们一一获取到index.php文件中。有没有更好的方法,比如 getID 函数不仅返回 $id,还可以返回其他四个参数?请指教。

最佳答案

可以选择以数组形式返回数据

function getID($url)
{
global $link;
$ch = curl_init("http://example.com/?id=".$url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
$raw = curl_exec($ch);
curl_close($ch);
//return the $raw json data as associative array
return json_decode($raw,true);
}

$myinfo=getID($url);
echo "My ID : " . $myinfo['id'];
echo "Client Name : " . $myinfo['client_info'];

等等...

关于php - 如何在不编写新函数的情况下从另一个文件调用变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26662326/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com