arianhojat Posted August 8, 2006 Share Posted August 8, 2006 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 10Inside function 10Change 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;?> Link to comment https://forums.phpfreaks.com/topic/16939-unset-doesnt-work-on-global-variables/ Share on other sites More sharing options...
Barand Posted August 8, 2006 Share Posted August 8, 2006 I suggest you read the manual againhttp://www.php.net/unset Link to comment https://forums.phpfreaks.com/topic/16939-unset-doesnt-work-on-global-variables/#findComment-71353 Share on other sites More sharing options...
arianhojat Posted August 8, 2006 Author Share Posted August 8, 2006 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:" Link to comment https://forums.phpfreaks.com/topic/16939-unset-doesnt-work-on-global-variables/#findComment-71396 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.