Forum Moderators: coopster
$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...
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
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!";
break;
}
}
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.".");
$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');
if($fp){
// do something with $fp
}
else{
// 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
}
else{
// 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");