Reset variables in PHP

They have: 105 posts

Joined: Jan 2002

Is it possible to reset variables to be empty in PHP? I have a script which runs fine unless the user goes back on the page.

Thanks in advance.

They have: 105 posts

Joined: Jan 2002

After a Pepsi break, once again I've answered my own question:

$reset_array = array();

Just set the array to an empty array when the line of code is reached. duh!

CPRhosting.com - CPR Hosting...Giving life to the web
Custom-made Hosting Plans starting at $2

Suzanne's picture

She has: 5,507 posts

Joined: Feb 2000

lol... Caffeine to the rescue!

Mark Hensler's picture

He has: 4,048 posts

Joined: Aug 2000

To destroy a variable (free the memory), use:
unset($var);

PHP Docs:
unset()

Argh! And now you've made me thirsty!

I'm not really Halle Berry. I'm Barry Bostwick!" Wink

Mark Hensler
If there is no answer on Google, then there is no question.

They have: 105 posts

Joined: Jan 2002

This was taken from php.net

[email protected]
26-Mar-2001 09:58

The docs for unset() should be changed in the following way:

unset() destroys the specified variables. This means, that the REFERENCE
to the VALUE is destroyed, not the VALUE itself (in PHP4 every variable is
a reference to a value, see chapter "references"). PHP4 cleans
up the VALUE memory automatically for you, if no references to a value are
left.

So be extremly careful if you unset() a variable with MORE THAN ONE
REFERENCE. Cause unset() clears only this reference, the remaining
references are not cleared! This means, that this results in different
values for two vars which has been referenced to one value after
unset()ing one of them.

The prefered way to tell PHP, that an referenced var should be cleared is
to set it to the value NULL. This will change the VALUE, and does not
delete the REFERENCE. Be careful, there are currently some (more or less
undocumented) discrepancies left in PHP, which will have perhaps bad side
effects.

Please have a closer look to the "references"-chapter to
understand what could happen.

PS: My personal opinion is, that PHP either lacks documentation or needs
some discussion in the way it interprets unset() and NULL and what will
happen with NULL-values.

CPRhosting.com - CPR Hosting...Giving life to the web
Custom-made Hosting Plans starting at $2

Mark Hensler's picture

He has: 4,048 posts

Joined: Aug 2000

They just went into a more advanced explanation of how the unset() function destroys variables.

I'm not an expert with this yet, but from what I've learned:

This acts the same way UNIX handles program text segments. Multiple instances of a program may run, but they will all share the same text segment. When the last intance of the program terminates, the text segment is then freed from memory.

$variable = "value";
$reference1 = &$variable;
$reference2 = &$variable;

"value" is stored in a data segment somewhere in memory. $variable, $reference1, and $reference2 all point to that memory address.

unset($reference1);

I've just blown up $reference1. $reference1 has now been freed from memory. But $variable and $reference2 still point to "value".

$reference2 = NULL;

I've just set all references to "value" to NULL. They all still have reserved memory, but no value.

Mark Hensler
If there is no answer on Google, then there is no question.

Want to join the discussion? Create an account or log in if you already have one. Joining is fast, free and painless! We’ll even whisk you back here when you’ve finished.