Welcome to WebmasterWorld Guest from

Forum Moderators: coopster & jatar k

Message Too Old, No Replies

unset() and session_unregister() in PHP

the difference

2:17 am on Jul 31, 2002 (gmt 0)

Senior Member from US 

WebmasterWorld Senior Member lorax is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month

joined:Mar 31, 2002
votes: 4

It took me the longest time to get a decent understanding of the difference between these two functions (well - technically unset() is a statement as it does not return a value when executed). I thought I might share what I've learned - (or share my misunderstanding and perhaps inspire a discussion :)) - in hopes that others might be inclined to pick up the thread and share some of thier insights on how a few of PHP's functions work and/or how they can be applied.

$foo = 23;

Unset() destroys the specified variables. This means, that the reference "$foo" to value "23" is destroyed, not the value "23" itself.

If I unset($foo); I destroy the reference but the value, 23, is still in memory albeit without anyway to refer to it. In general, don't worry about it since PHP4 will clean the value out of memory though I'm not clear on when or how.

session_unregister() de-registers (read: forgets) the variable from the current session. This function only prevents the variable from being saved as part of the session.

So session_unregister($foo); tells the session not to remember $foo. But $foo is still alive and will follow the rules for global/local variables unless you use unset().

So in the case of unset() - it's a variable terminator - if you want to be sure that the variable is gone use unset(). If you only want to drop the variable from the session but retain it for a bit more work use session_unregister().

Now...if I'm wrong - I hope to get it right once-and-for-all! Also - feel free to elaborate...


5:48 pm on July 31, 2002 (gmt 0)

Inactive Member
Account Expired


$foo = 23;
echo "$foo"; // returns 23

session_register( "foo" ); // registers $foo you can also use 'print session_encode();' to see whats set

session_unset(); // drops all the set cookies(ie. $foo)

echo "$foo"; // returns NULL

hope this will also help

10:36 pm on July 31, 2002 (gmt 0)

Junior Member

10+ Year Member

joined:May 2, 2002
votes: 0

That is right.

I use the combination of unset($var); session_unregister("var"); to make myself absolutely sure that $var is destroyed and there won't be any problem to assing it a new value.

Sessions are sooo cool! Aren't they? :)

3:30 am on Aug 1, 2002 (gmt 0)

Senior Member from US 

WebmasterWorld Senior Member lorax is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month

joined:Mar 31, 2002
votes: 4

Sessions are cool.

I'm working on an Intranet app that let's our CEO manage his upper management. Essentially a CRM application which allows him to make sure they're doing thier jobs and selling our services - and if not, he can yank thier strings very easily.

What I find challenging - and indeed most rewarding when it comes together seamlessly - is the puzzle. The app is in it's early stage and I've basically banged it out without much streamlining. The fun part is taking raw functional code and making it elegant and streamlined. Which often involves learning the nuances of a function I didn't know before. Sometimes an epiphany occurs and I get to see the whole puzzle before me.

Which is the self-serving side of starting this thread. I know there are other coders lurking here who have a few insights into PHP.

What about break; and exit;

break; will terminate the current loop and jump to the next line of script outside the loop.

exit; will terminate the entire script.

break; is great for evaluating the contents of an array using a "for" loop

for ($i=0; $i<$rows; $i++) {
$employee = mysql_fetch_array($query_result);
if ($employee["name"] == "Bob") {
echo "You found Bob!";

And exit; is a great way to trap errors - it allows you to provide the user with a comment before the script cancels. For example:

$file = fopen ($filename, 'r')
or exit("Sorry, but I can't find the file ".$filename.".");

12:04 pm on Aug 2, 2002 (gmt 0)

Preferred Member

10+ Year Member

joined:June 7, 2002
votes: 0

A synonym of exit() is die(), which looks kind of cool:

$fp=fopen("test.txt", 'w') or die("Couldn't open test.txt");

Indeed, this works because fopen returns false is the file couldn't be opened for some reason. We could write a script like this:

$fp=fopen("test.txt", 'w');
// do something with $fp
// handle the error

But actually, the assignment itself returns the value assigned to $fp, so we could combine the first two lines, and do it this way:

if($fp=fopen("test.txt", 'w')){
// do something with $fp
// handle the error

It will work in exactly the same way. Note that the condition uses = instead of ==: that's because we are not merely checking for equality, but we are actually performing an assignment, and then checking the value that was assigned. What a difference one = makes!

Often you can replace if-else blocks with the ternary operator. For example, this code:

print("$num_items item");
if($num_items==1) print("s");
print(" in stock");

can be written:

print("$num_item item"+($num_items==1)?"s":""+" in stock");

There's a subtle difference between print() and echo. print() is a function, echo is a language construct. So this will work:

$some_boolean ? print("true") : print("false");

but this won't:

$some_boolean ? echo("true") : echo("false");


Join The Conversation

Moderators and Top Contributors

Hot Threads This Week

Featured Threads

Free SEO Tools

Hire Expert Members