homepage Welcome to WebmasterWorld Guest from
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
Forum Library, Charter, Moderators: coopster & jatar k

PHP Server Side Scripting Forum

unset() and session_unregister() in PHP
the difference

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

Msg#: 534 posted 2:17 am on Jul 31, 2002 (gmt 0)

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...



10+ Year Member

Msg#: 534 posted 5:48 pm on Jul 31, 2002 (gmt 0)

$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+ Year Member

Msg#: 534 posted 10:36 pm on Jul 31, 2002 (gmt 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? :)


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

Msg#: 534 posted 3:30 am on Aug 1, 2002 (gmt 0)

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.".");


10+ Year Member

Msg#: 534 posted 12:04 pm on Aug 2, 2002 (gmt 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");

Global Options:
 top home search open messages active posts  

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved