Jump to content


Photo

unset() doesnt work on global variables?


  • Please log in to reply
2 replies to this topic

#1 arianhojat

arianhojat
  • Members
  • PipPipPip
  • Advanced Member
  • 235 posts

Posted 08 August 2006 - 07:35 PM

I use a variable $x inside my function. the $x that it is refering to is the global one, as it is declared by global keyword. but unsetting a global variable seem to not unset the variable outside of the function. is this true?

here is an example. prints out:

Before function 10
Inside function 10
Change Inside function 20
Unset Inside function (<<<<-PHP Notice:  Undefined variable)
Outside function 20 (Should be undefined here, right?)

<?php
$x = 10;

echo "\nBefore function ". $x;

function change(){
global $x;
echo "\nInside function ". $x;

$x = 20;
echo "\nChange Inside function ". $x;

unset($x);
echo "\nUnset Inside function ". $x;

}
change();
echo "\nOutside function ". $x;

?>

#2 Barand

Barand
  • Moderators
  • Sen . ( ile || sei )
  • 18,021 posts

Posted 08 August 2006 - 07:53 PM

I suggest you read the manual again

http://www.php.net/unset
If you are still using mysql_ functions, STOP! Use mysqli_ or PDO. The longer you leave it the more you will have to rewrite.

Donations gratefully received






moon.png

|baaGrid| easy data tables - and more
|baaChart| easy line, column and pie charts

#3 arianhojat

arianhojat
  • Members
  • PipPipPip
  • Advanced Member
  • 235 posts

Posted 08 August 2006 - 08:58 PM

doh i was a little woozy at the time reading it, so i glanced at the following lines and read right by them heh.
"If a globalized variable is unset() inside of a function, only the local variable is destroyed. The variable in the calling environment will retain the same value as before unset() was called.
...
If you would like to unset() a global variable inside of a function, you can use the $GLOBALS array to do so:
"




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users