dse55384 2014-10-30 20:07
浏览 83
已采纳

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

How can I call a variable from another file? Is it possible for a function to return 5 values? In my function.php file I grab a lot of values in different variables. For instance lets look at the function below
Function.php file

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 file

  $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    : "

I do not want to keep everything in one file. In the index.php file, I also want to output 'cname', 'info', 'up', 'cat' values under 'myid'. I was thinking of making 4 different functions and get them one by one in the index.php file. Is there a better way like instead of returning only $id, the getID function can return the other four parameters too? Please advice.

  • 写回答

2条回答 默认 最新

  • drzk21632 2014-10-30 20:14
    关注

    return the data as an array is an option

    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'];
    

    etc...

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器