Page 1 of 1

arrays and resources

Posted: Fri Jul 05, 2002 1:50 pm
by hob_goblin
now that I think about it, everytime I run a script there are alot of arrays being passed..

$_GET
$_POST
$_COOKIE

and i can also still use

$HTTP_GET_VARS
$HTTP_POST_VARS
$HTTP_COOKIE_VARS

im just wondering, are these arrays made everytime the script is accessed?
would I get anything worthwhile using unset() to clear up some useless information at the begining of larger scripts?

Posted: Fri Jul 05, 2002 2:04 pm
by RandomEngy
Well, in my experience, the $_POST array only contains information that was submitted to that page, and nothing from any previous submissions, so I'd doubt you'd gain anything there. I suspect the $_GET array is similar, and I don't know anything about cookies.

Posted: Fri Jul 05, 2002 2:08 pm
by hob_goblin
yeah, i realize that.. but I mean $_POST and $HTTP_POST_VARS are exactly alike, and on large scripts it would seem like just deleting duplicates might reduce clutter... (i mean if a good 10k is being submitted by a user, and 50 users are submitting at once, it could add up..)

Posted: Fri Jul 05, 2002 2:23 pm
by RandomEngy
Ahh I see what you mean. Still, even if they are separate arrays, I don't see much gain to be had over it. Wouldn't it just go: PHP gets the information and puts it in two identical arrays, the script runs to create the page, and then both arrays are destroyed. So you'd be gaining a little bit of memory for the duration the script runs, but I don't think it's worth it.

And correct me if I'm wrong about the arrays getting deleted after the script finishes.

Posted: Fri Jul 05, 2002 2:40 pm
by BDKR
You know, I'm willing to bet (or in other words, I'm making a kinda educated guess) that $HTTP_POST_VARS and $_POST are the same only in name and reference the same bit of memory space in the system.

So $HTTP_POST_VARS[joe_sixpack] and $_POST[joe_sixpack] are actually the same bit of memory space as opposed to one being a copy (or duplicate) of another. And of course, the frienly folks at PHP are allowing the continued, though deprecated, use of $HTTP_POST_VARS for slow tossers like me that can't get a big project done in under a year :!: Good thing I'm not getting paid for this one eh?

Besides, I can't see these guys justifying the creation of duplicate arrays. Most people are either going to use one or the other.

What do you think? Anybody feel like testing this? I'm compiling in shared memory support at this time so I'm out.

Later on,
BDKR (TRC)

Posted: Fri Jul 05, 2002 2:49 pm
by protokol
$HTTP_*_VARS is simply a reference to the $_* var

Posted: Fri Jul 05, 2002 2:57 pm
by hob_goblin
thanks, you learn something new every day ;)

Posted: Fri Jul 05, 2002 3:00 pm
by protokol
Definitely use $_* instead of $HTTP_*_VARS though, because the parser sees $_* vars a lot quicker than it does $HTTP_*_VARS (more chars to test)

Posted: Fri Jul 05, 2002 3:11 pm
by llimllib
Referencing is a clutch concept to understand. If you have:

Code: Select all

function doSomething($bigArray) {//do something}
and you call it like this:

Code: Select all

doSomething($myBigArray);
you're creating a copy of your array, which is slow and takes up a lot of memory. The changes made to the array will also not be saved, only used inside the function doSomething. However, if you declare:

Code: Select all

function doSomething(&$bigArray) {//do something}
and:

Code: Select all

doSomething(&$myBigArray)
you have only passed a reference to the contents of myBigArray, which is much smaller and faster. Keep in mind, now any changes to $bigArray will be reflected in $myBigArray, as they are two names for the same thing.

Reference: http://www.php.net/manual/en/language.references.php

Posted: Fri Jul 05, 2002 3:14 pm
by BDKR
Hey llimlilb,

Good post G! This is one of those areas that's like a minefield. I coded for eons before I paid serious attention to this.

What a dork!

later on,
BDKR (TRC)

Posted: Fri Jul 05, 2002 3:18 pm
by llimllib
a couple of C programming classes will make you start to think about references real hard real fast :)

Posted: Fri Jul 05, 2002 3:28 pm
by BDKR
It's funny that you mention that as it's becuse of my interest in C and Objective C that I started to really pay attention to the stuff. PHP makes it easy for us when it comes to dealing with memory.

Later on,
BDKR (TRC)