4

I have a function called init on my website in an external file called functions.php. Index.php loads that page and calls function init:

function init(){
   error_reporting(0);
   $time_start = microtime(true);
   $con = mysql_connect("localhost","user123","password123");
   mysql_select_db("db123");
}

How can I get all of these variables global(if possible) without having to use

global $time_start;
global $con;
0

4 Answers 4

3

You don't want it global.

On alternative is to encapsulate it in a object or even use a DI in order to configure it.

Sign up to request clarification or add additional context in comments.

Comments

1

If you want to specifically use globals, take a look at $GLOBALS array. Even though there are couple of other ways, Pass by reference, Data Registry Object recommended, etc.

More on variable scopes.

Comments

1

Maybe you can return these variables from your init() function :

function init(){
   error_reporting(0);
   $time_start = microtime(true);
   $con = mysql_connect("localhost","user123","password123");
   mysql_select_db("db123");

   return array('time_start' => $time_start, 'con' => $con);
}

Comments

0

You can declare them in the global scope, then pass them to the function by reference. After modifying the function to do so.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.