NYCPHP Meetup

NYPHP.org

[nycphp-talk] global variables

Hans Zaunere zaunere at yahoo.com
Thu May 22 20:18:05 EDT 2003


--- "Malcolm, Gary" <gmalcolm at professionalcredit.com> wrote:
> Whoa...
> 
>  never even thought of appending the GLOBALS array!
> 
> on the same subject:
> 	does php get the same performace boost from referencing large arrays
> instead of passing them like C?

Ehh, well, sort of.  References in PHP are not pointers in C, although they
act similarly sometimes :)

> ex:
> 	$fatarray = array(5 Megs o' stuff);
> 	sortFatArray($fatarray);
> 
> 	function sortFatArray(&$fattyref){
> 		sort($fattyref);
> 	}

In this case, since sort() modifies it's argument, if you wanted to sort the
global $fatarray you would need to declare the user function as accepting a
reference (with the ampersand), as well.  Otherwise, you'd end up with 10megs
of memory resident array.

On the other hand, if you are only reading the array in your user function,
it's better not to use a reference.

H


> 
> 
> 
> > -----Original Message-----
> > From: Hans Zaunere [mailto:zaunere at yahoo.com]
> > Sent: Thursday, 22 May, 2003 3:55 PM
> > To: NYPHP Talk
> > Subject: RE: [nycphp-talk] global variables
> > 
> > 
> > 
> > --- "Malcolm, Gary" <gmalcolm at professionalcredit.com> wrote:
> > > I've been told best practice is to NOT use globals in 
> > functions, if simply
> > > for readability but I bet we all do :) . Also, I wonder if a passed
> > > reference to a variable is as efficient... does anyone have 
> > an opinion on
> > > which of these is better and why (as a general rule)
> > > 1)
> > > 
> > > 	$var foo;
> > > 	function  bar(&$foo){
> > > 		do stuff to change $foo
> > > 	}
> > > 
> > > 2)
> > > 	var $foo = bar();
> > > 	function bar(){
> > > 		do stuff
> > > 		return a value for $foo
> > > 	}
> > > 
> > > 3)
> > > var $foo;
> > > bar();
> > > function bar(){
> > > 	global $foo;
> > > 	do stuff to change $foo
> > > }
> > 
> > From various dealings and bugging people, I understand that this is
> > preferred:
> > 
> > 
> > function createAglobal() {
> >    $GLOBALS['myglobal'] = 'The Value';
> > }
> > 
> > /** Fast **/
> > function readAglobal() {
> >    echo "This is my global: {$GLOBALS['myglobal']}";
> > }
> > 
> > /** A *tiny* bit slower, but more readable if the global is 
> > used a lot **/
> > function readAglobal2() {
> >    $nochange = $GLOBALS['nochange'];
> > 
> >    echo "This is my global: $nochange";
> > }
> > 
> > function changeAglobal() {
> >    $GLOBALS['yeschange'] .= "Appending this string";
> >    echo "This changed the global var:  {$GLOBALS['yeschange']}";
> > }
> > 
> > function changeAglobal2() {
> >    $yeschange = &$GLOBALS['yeschange'];
> >    $yeschange .= "Appending this string";
> >    echo "This changed the global var:  $yeschange";
> > }
> > 
> > Or any variation thereof - YMMV  :)
> > 
> > 
> > H
> > 
> > 
> > 
> > 
> > 
> > 
> > 
> > 
> 
> 
> 
> --- Unsubscribe at http://nyphp.org/list/ ---
> 
> 




More information about the talk mailing list