0

I am forever going up and down my code adding and removing globals to functions.

Is it possible to pass global variables inside global variables to functions to save defining them in each time in function? If not is there a better way to do this?

My aim is to have an index of global variables at the top of each page and not worry about having to defining them inside functions.

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 

1 Answer 1

2

Im not sure if I understood well. But why don't you make $site and array instead of a string ?

Like this :

$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 } 
Sign up to request clarification or add additional context in comments.

1 Comment

ill check this out, did some further research into this, it seems its not best practice as you can loose track of which variables are being used as globals unless you list them at the top of each function; if you do that you might as well just define them. This would be useful if you were using EXACTLY the same variables over and over again in loads of functions. Thanks again theo +1.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.