RE: [PHP] using global to pass vars to functions
| From: | Anthony Martin | Date: | Fri, 01 Dec 2000 20:47:41 +0000 |
| Subject: | RE: [PHP] using global to pass vars to functions | ||
| Groups: | php.general | ||
| Request: | Send a blank email to php-general+get-28285@lists.php.net to get a copy of this message | ||
I did mess that HTML up. It should have read:
<FORM>
<INPUT type="text" name="profile['ID']">
<INPUT type="password" name="profile['PASSWD']">
<INPUT type="text" name="profile['EMAIL']">
</FORM>
Names not values...gotta remember...
Anthony
-----Original Message-----
From: Anthony Martin [mailto:Anthony@TRAMS.com]
Sent: Friday, December 01, 2000 12:46 PM
To: 'Andy Clarke'; php-general@lists.php.net
Subject: RE: [PHP] using global to pass vars to functions
I kind of do that when I use functions. But mostly, where I need global
vars, I write my "function" into it's own include file and call the include
right there.
Another way to go is to encapsulate all of your values into an associative
array. You can even do this in an HTML form:
<FORM>
<INPUT type="text" value="profile['ID']">
<INPUT type="password" value="profile['PASSWD']">
<INPUT type="text" value="profile['EMAIL']">
</FORM>
Then you can pass the $profile array to any function -or- call it global
with ease.
Anthony
-----Original Message-----
From: Andy Clarke [mailto:andy@kinonet.com]
Sent: Friday, December 01, 2000 12:24 PM
To: php-general@lists.php.net
Subject: [PHP] using global to pass vars to functions
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
-----------------------------
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, e-mail: php-general-unsubscribe@lists.php.net
For additional commands, e-mail: php-general-help@lists.php.net
To contact the list administrators, e-mail: php-list-admin@lists.php.net
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, e-mail: php-general-unsubscribe@lists.php.net
For additional commands, e-mail: php-general-help@lists.php.net
To contact the list administrators, e-mail: php-list-admin@lists.php.net