RE: [PHP] unset Fuction

From: Date: Tue, 10 Feb 2004 12:57:23 +0000
Subject: RE: [PHP] unset Fuction
Groups: php.general 
Request: Send a blank email to php-general+get-177275@lists.php.net to get a copy of this message
Hello John, > From: JohnT [mailto:johntamares@hotmail.com] > Sent: Tuesday, February 10, 2004 4:54 AM > > Hello, > > mind explain a little bit further cause i'm confused and not > sure if the > fuction really work or something > > [some Code] As far as I know (or understand), unset() will only destroy the reference to a variable. That's why you get sometimes a bit confusing results when unset variables insinde functions, because they are often just a reference or even a copy of another variable. (for example using global $a; is equal to $a =& $GLOBALS['a']) See: Chapter References Explained -> Unsetting References and Function Reference of unset() of the php Manual. Now I guess that a static variable is only available via reference in a function call. Otherwise it would be unseted if the function call is finished, what's not the use of a static var. How to fix this problem: The purpose of your code was to reset the value of the static variable. So just do this reseting by assigning the value you wanted, e.g. /////////////// CODE ///////////////// function foo() { static $a; $a++; echo "$a<BR>"; $a = 0; } foo(); foo(); foo(); /////////////////////////////////////////////// Now it outputs: 1 1 1 Regards, Dirk Oschlies

« previous php.general (#177275) next »