NYCPHP Meetup

NYPHP.org

[nycphp-talk] slow php

David Krings ramons at gmx.net
Mon Feb 16 19:44:32 EST 2009


Nicholas Hart wrote:
> Many thanks for the considerable response!  I have changed the code to 
> reduce a large number of SELECTs by saving a query to an array.  My 
> problem now is that although this array is appox. 3,000 records, I need 
> a way to pass this to a function.  Note: the array contains all discount 
> data while the function pulls out several sub-queries per user and 
> product codes.  Since there are 5 sub queries, it is probably easier to 
> pass the main one. 
> 
> Any idea how to pass this array, a pointer to it or make this a global 
> array?  Making a global array appears to be depricated or discouraged.  
> I have tried passing this array which is called $discount but it doesn't 
> appear to be working when I do.  I also run out of memory which at 128MB 
> is kind of odd. 
> 
> Any help is much appreciated..


One note, please add at least the subject of the thread in when replying to a 
section of a digest email and puleaze trim messages. 90% of your email had 
nothing to do with what you were asking. I won't even get into the top posting 
issue....


Now, by default PHP passes function arguments by value. That means it makes a 
copy of what you specified as argument and works with that copy. That means if 
you pass an array and change an element from within the function the array is 
changed only within the scope of the function. In order to have the original 
be changed as well you can either return the changed array and then assign it 
to the original. The more convenient way is to pass the array by reference, 
because then the function will not make a copy and apply changes to the original.

You find it nicely explained with examples in the PHP manual, especially this 
portion:
http://www.php.net/manual/en/functions.arguments.php

I must say that the PHO manual is one of the best manuals ever written and the 
web version is even better with all the comments added...although with those 
it happens that some are plain wrong and others are just so old that they no 
longer apply.

Way back when PHP made everything global, which was easier to some extent, but 
really made for sloppy and often incompatible programming. Also, the "&" is 
easily overlooked, so I recommend to mention in the function desciption 
commentary that this array is passed in by reference and what will happen to 
the array passed when using this function.


Crafting very wordy responses is also frowned upon, I take the blame for that one.

David



More information about the talk mailing list