0

我一直在上下我的代码添加和删除函数的全局变量。

是否可以将全局变量中的全局变量传递给函数以保存在函数中每次定义它们?如果没有,有没有更好的方法来做到这一点?

我的目标是在每一页的顶部都有一个全局变量的索引,而不必担心必须在函数中定义它们。

global $site;
$site = "global $link, $create, $populate, $delete, $withdraw";

$host = "";
$username = "";
$password = "";
$database = "";

$link = mysqli_connect("$host", "$username", "$password", "$database");

function display() { global $site; // code }
function populate() { global $site; // code }
function create() { global $site; // code }
function withdraw() { global $site; // code }

    //output
4

1 回答 1

2

我不确定我是否理解得很好。但是你为什么不制作$site和数组而不是字符串呢?

像这样 :

$host = "";
$username = "";
$password = "";
$database = "";

$link = mysqli_connect("$host", "$username", "$password", "$database");

$site = array(
  'link'     => $link,
  'create'   => $create,
  'populate' => $populate,
  'delete'   => $delete,
  'withdraw' => $withdraw,
);

function display() {
  global $site;
  // Here you can access & modify your $site array
}
于 2013-05-25T16:11:40.117 回答