using global to pass vars to functions

From: Date: Fri, 01 Dec 2000 20:23:47 +0000
Subject: using global to pass vars to functions
Groups: php.general 
Request: Send a blank email to php-general+get-28278@lists.php.net to get a copy of this message
I am working on a moderately large website, running from a mySQL database. As the site will be added to, upgraded and redesigned once it is up, I want to separate the HTML and the PHP code as much as possible and to keep the PHP functions, etc. left embedded in the pages as simple as possible. I have moved all of the functions to shared include files. In general, each web page calls a function to retreive data from a mySQL database and puts this information into variables; later in the page, the contents of these variables are displayed via echo statements. I know that it is a little unconventional, but is there any problems or drawbacks (regarding performance, etc.) in using global to get values into and out of the function, rather than sending and receiving them as parameters? Or is it better to keep the number of global variables used to a minimum? What I mean is using a function like this: function find_things () { global $var_in1, $var_in2, $var_out1, $var_out2; ... perform mysql query ... } and calling it from the web page like this: <?php find_things (); ?> Rather than having a function like this: function find_things ($var_in1, $var_in2) { ... perform mysql query ... return array ($var_out1, $var_out2); } and calling it like this: <?php list ($var_out1, $var_out2) = find_things ($var_in1, $var_in2); ?> It seems to me that this would make the function calling code much simpler, and less likely to be altered accidentally when the pages are redesigned. It would also mean that I don't have to change every line that calls the function (in every page) when the function is rewritten or developed and, for examle, changes the number or order of parameters that it takes. By using globals, the only file that gets changed is the PHP include with the function in. The system for naming variables is consistent throughout the whole site. Thanks Andy Clarke ----------------------------- Andy Clarke 78 West Kensington Court Edith Villas London W14 9AB Phone: 44 (0)20 7602 3382 Mobile: 07947 418177 Email: andy@kinonet.com -----------------------------

« previous php.general (#28278) next »