我永远在我的代码上下添加和删除全局函数。
是否有可能通过全局变量内部的全局变量函数,以节省在每次函数中定义它们?如果没有,有没有更好的方法?
我的目标是在每个页面的顶部有一个全局变量的索引,而不用担心必须在函数内部定义它们。
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
我不确定我是否理解得很好。但是你为什么不让$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
}