arrays and resources

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
hob_goblin
Forum Regular
Posts: 978
Joined: Sun Apr 28, 2002 9:53 pm
Contact:

arrays and resources

Post 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?
User avatar
RandomEngy
Forum Contributor
Posts: 173
Joined: Wed Jun 26, 2002 3:24 pm
Contact:

Post 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.
User avatar
hob_goblin
Forum Regular
Posts: 978
Joined: Sun Apr 28, 2002 9:53 pm
Contact:

Post 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..)
User avatar
RandomEngy
Forum Contributor
Posts: 173
Joined: Wed Jun 26, 2002 3:24 pm
Contact:

Post 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.
User avatar
BDKR
DevNet Resident
Posts: 1207
Joined: Sat Jun 08, 2002 1:24 pm
Location: Florida
Contact:

Post 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)
User avatar
protokol
Forum Contributor
Posts: 353
Joined: Fri Jun 21, 2002 7:00 pm
Location: Cleveland, OH
Contact:

Post by protokol »

$HTTP_*_VARS is simply a reference to the $_* var
User avatar
hob_goblin
Forum Regular
Posts: 978
Joined: Sun Apr 28, 2002 9:53 pm
Contact:

Post by hob_goblin »

thanks, you learn something new every day ;)
User avatar
protokol
Forum Contributor
Posts: 353
Joined: Fri Jun 21, 2002 7:00 pm
Location: Cleveland, OH
Contact:

Post 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)
User avatar
llimllib
Moderator
Posts: 466
Joined: Mon Jul 01, 2002 2:19 pm
Location: Baltimore, MD

Post 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
Last edited by llimllib on Fri Jul 05, 2002 3:15 pm, edited 1 time in total.
User avatar
BDKR
DevNet Resident
Posts: 1207
Joined: Sat Jun 08, 2002 1:24 pm
Location: Florida
Contact:

Post 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)
User avatar
llimllib
Moderator
Posts: 466
Joined: Mon Jul 01, 2002 2:19 pm
Location: Baltimore, MD

Post by llimllib »

a couple of C programming classes will make you start to think about references real hard real fast :)
User avatar
BDKR
DevNet Resident
Posts: 1207
Joined: Sat Jun 08, 2002 1:24 pm
Location: Florida
Contact:

Post 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)
Post Reply