By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
438,278 Members | 1,359 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 438,278 IT Pros & Developers. It's quick & easy.

It seems a Php BUG to me

P: n/a
PHP Version 4.3.9
(the latest version for my centos distro)

f1();

function f1()
{
global $a;

f2();

var_dump($a); //NULL??? It would be "hi", wouldn't
it?
}

function f2()
{
global $a;

unset($a);

$a = "hi";

var_dump($a); //String "hi", correct
}
Aug 13 '08 #1
Share this Question
Share on Google+
2 Replies


P: n/a
"preso" <op**@asis.aawrote in message
news:MP************************@news.libero.it...
PHP Version 4.3.9
(the latest version for my centos distro)
f1();
function f1()
{
global $a;
f2();
var_dump($a); //NULL??? It would be "hi", wouldn't
it?
}
function f2()
{
global $a;
unset($a);
$a = "hi";
var_dump($a); //String "hi", correct
}
Try it like this...

$a="";
f1();
function f1() {
global $a;
f2();
var_dump($a);
}

function f2(){
global $a;
// - no need to unset
$a="hi";
var_dump($a);
}

So that $a exists before the call to f1()

Aug 13 '08 #2

P: n/a
*** preso escribió/wrote (Wed, 13 Aug 2008 12:55:56 GMT):
f1();

function f1()
{
global $a;

f2();

var_dump($a); //NULL??? It would be "hi", wouldn't it?
}

function f2()
{
global $a;

unset($a);

$a = "hi";

var_dump($a); //String "hi", correct
}
I think your code behaves as documented:

1.- global $a creates a local variable that refers to the global variable
(we have two vars pointing to the same place).

2.- unset($a) only destroys the local variable. The global variable still
exists. To remove the global variable we'd need to do unset($GLOBALS['a'])

3.- $a = "hi" creates a new local variable (it didn't exist because we've
removed it). It's unrelated to the global variable because we broke the
link.

--
-- http://alvaro.es - Álvaro G. Vicario - Burgos, Spain
-- Mi sitio sobre programación web: http://bits.demogracia.com
-- Mi web de humor en cubitos: http://www.demogracia.com
--
Aug 13 '08 #3

This discussion thread is closed

Replies have been disabled for this discussion.