Bluehost.com Web Hosting $6.95

Re: Globals

This is a discussion on Re: Globals within the PHP General forums, part of the PHP Programming Forums category; Hi Chris, Chris Boget wrote: > function blah() { > //global $GLOBALS; > echo 'Globals: <pre>'; print_r( $GLOBALS ); echo '&...


Go Back   Usenet Forums > PHP Programming Forums > PHP General

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 08-01-2003
Greg Beaver
 
Posts: n/a
Default Re: Globals

Hi Chris,

Chris Boget wrote:
> function blah() {
> //global $GLOBALS;
> echo 'Globals: <pre>'; print_r( $GLOBALS ); echo '</pre>';
>
> }
>
> As it is shown above, with the 'global $GLOBALS' line commented
> out, the print_r() works and shows all the currently defined variables
> and their corresponding values. However, if I declare $GLOBALS
> as global, nothing gets printed out.
>
> Why?


the global $GLOBALS directive is the equivalent of this PHP code:

$GLOBALS = &$GLOBALS['GLOBALS'];

$GLOBALS does not contain a reference to itself, so you are essentially
setting $GLOBALS to refer to nothing! I'm not sure it is a great idea
ot allow this, perhaps the internals folks can answer on that one.

> Wouldn't the 'global $GLOBALS' line be more or less redundant in
> most cases? Because $GLOBALS is a superglobal (though, it isn't
> really)? Why would it affect whether or not $GLOBALS actually has


It appears that $GLOBALS is not a superglobal because a superglobal
always has an index in the $GLOBALS array.

> While I'm on this subject, why isn't $GLOBALS always a superglobal?
> For example, this doesn't work:
>
> function innerFunc() {
>
> echo $GLOBALS['blah'];
>
> }
>
> function outerFunc() {
>
> innerFunc();
>
> }
>
> $blah = 'bob';
> outerFunc();
>
> Nothing gets echoed from innerFunc(). Why? If anyone can offer
> any insight as to what is going on, I'd be ever so appreciative.


This code worked for me, I saw "bob." Did you post the example you
meant to?

Greg
--
phpDocumentor
http://www.phpdoc.org

Reply With Quote
  #2 (permalink)  
Old 08-01-2003
Greg Beaver
 
Posts: n/a
Default Re: [PHP] Re: Globals

Try this code:

<?php
function blah() {
var_dump($GLOBALS['GLOBALS']);

}

blah();
?>

It appears that in a function scope, it doesn't. This is definitely a
bug, I'll post it if it hasn't already been noticed.

Greg

Leif K-Brooks wrote:

> Greg Beaver wrote:
>
>> $GLOBALS does not contain a reference to itself

>
>
> Yes it does. I just ran the following, and I got "Greg is wrong."
>
> <?php
> $foo = 'Greg is wrong.';
> echo $GLOBALS['GLOBALS']['GLOBALS']['GLOBALS']['foo'];
> ?>
>


Reply With Quote
  #3 (permalink)  
Old 08-01-2003
Jim Lucas
 
Posts: n/a
Default Re: [PHP] Re: Globals

actually, it does work and it does exist.

Try using print_r() or

print_r(array_keys($GLOBALS));


and you will see an entry for GLOBALS

mine is located at #13

Jim Lucas

----- Original Message -----
From: "Greg Beaver" <greg@chiaraquartet.net>
To: "Leif K-Brooks" <eurleif@buyer-brokerage.com>
Cc: <php-general@lists.php.net>
Sent: Friday, August 01, 2003 12:45 PM
Subject: Re: [php] Re: Globals


> Try this code:
>
> <?php
> function blah() {
> var_dump($GLOBALS['GLOBALS']);
>
> }
>
> blah();
> ?>
>
> It appears that in a function scope, it doesn't. This is definitely a
> bug, I'll post it if it hasn't already been noticed.
>
> Greg
>
> Leif K-Brooks wrote:
>
> > Greg Beaver wrote:
> >
> >> $GLOBALS does not contain a reference to itself

> >
> >
> > Yes it does. I just ran the following, and I got "Greg is wrong."
> >
> > <?php
> > $foo = 'Greg is wrong.';
> > echo $GLOBALS['GLOBALS']['GLOBALS']['GLOBALS']['foo'];
> > ?>
> >

>
>
> --
> PHP General Mailing List (http://www.php.net/)
> To unsubscribe, visit: http://www.php.net/unsub.php
>
>


Reply With Quote
  #4 (permalink)  
Old 08-01-2003
Greg Beaver
 
Posts: n/a
Default Re: [PHP] Re: Globals

Hi Jim,

The code you posted is correct, I never contested that. Read carefully.

<?php
function not_super()
{
var_dump(isset($GLOBALS['GLOBALS']));
$a = array_keys($GLOBALS);

var_dump(isset($a['GLOBALS']));
}
not_super();
?>

run this code, don't read it, you will see output of bool(false),
bool(false)

However, the following code works as expected!

<?php
function not_super()
{
return array_keys($GLOBALS);
}
var_dump(not_super(), array_keys($GLOBALS));
?>

GLOBALS will be present in both arrays.

Very odd behavior.

Greg


Jim Lucas wrote:

>actually, it does work and it does exist.
>
>Try using print_r() or
>
>print_r(array_keys($GLOBALS));
>
>
>and you will see an entry for GLOBALS
>
>mine is located at #13
>
>Jim Lucas
>
>----- Original Message -----
>From: "Greg Beaver" <greg@chiaraquartet.net>
>To: "Leif K-Brooks" <eurleif@buyer-brokerage.com>
>Cc: <php-general@lists.php.net>
>Sent: Friday, August 01, 2003 12:45 PM
>Subject: Re: [php] Re: Globals
>
>
>
>
>>Try this code:
>>
>><?php
>>function blah() {
>> var_dump($GLOBALS['GLOBALS']);
>>
>>}
>>
>>blah();
>>?>
>>
>>It appears that in a function scope, it doesn't. This is definitely a
>>bug, I'll post it if it hasn't already been noticed.
>>
>>Greg
>>
>>Leif K-Brooks wrote:
>>
>>
>>
>>>Greg Beaver wrote:
>>>
>>>
>>>
>>>>$GLOBALS does not contain a reference to itself
>>>>
>>>>
>>>Yes it does. I just ran the following, and I got "Greg is wrong."
>>>
>>><?php
>>>$foo = 'Greg is wrong.';
>>>echo $GLOBALS['GLOBALS']['GLOBALS']['GLOBALS']['foo'];
>>>?>
>>>
>>>
>>>

>>--
>>PHP General Mailing List (http://www.php.net/)
>>To unsubscribe, visit: http://www.php.net/unsub.php
>>
>>
>>
>>

>
>
>
>
>
>


Reply With Quote
  #5 (permalink)  
Old 08-01-2003
Greg Beaver
 
Posts: n/a
Default Re: [PHP] Re: Globals

Jim,

My bad, I posted the wrong code. Try this instead to see the bug:

<?php
function not_super()
{
var_dump(isset($GLOBALS['GLOBALS']));
return array_keys($GLOBALS);
}
//var_dump(not_super(), array_keys($GLOBALS));
not_super();
not_super();


?>

run this code, don't read it, you will see output of bool(false),
bool(false)

However, the following code works as expected!

<?php
function not_super()
{
return array_keys($GLOBALS);
}
var_dump(not_super(), array_keys($GLOBALS));
?>

GLOBALS will be present in both arrays.

Very odd behavior.

Note that in PHP 5.0.0b1, the first function does work, so this is fixed
somewhere in there.

Greg
--
phpDocumentor
http://www.phpdoc.org

Jim Lucas wrote:

>umm... That is what is going to happen when you follow your example.
>
>I think you are mistaken in what you are expecting...
>
>This line:
> $a = array_keys($GLOBALS);
>
>will give you an array of values that match the keys of the $GLOBALS array
>
>it won't copy the arrays.
>
>Try this.
>
><PRE>
><?php
>$a = array_keys($GLOBALS);
>foreach($a AS $key => $value) {
> echo $key."=>(".$value.")\n";
>}
>?>
>
>Jim Lucas
>
>----- Original Message -----
>From: "Greg Beaver" <greg@chiaraquartet.net>
>To: "Jim Lucas" <phplist@zonedzero.net>
>Cc: "Leif K-Brooks" <eurleif@buyer-brokerage.com>;
><php-general@lists.php.net>
>Sent: Friday, August 01, 2003 1:42 PM
>Subject: Re: [php] Re: Globals
>
>
>
>
>>Hi Jim,
>>
>>The code you posted is correct, I never contested that. Read carefully.
>>
>><?php
>>function not_super()
>>{
>> var_dump(isset($GLOBALS['GLOBALS']));
>> $a = array_keys($GLOBALS);
>>
>> var_dump(isset($a['GLOBALS']));
>>}
>>not_super();
>>?>
>>
>>run this code, don't read it, you will see output of bool(false),
>>bool(false)
>>
>>However, the following code works as expected!
>>
>><?php
>>function not_super()
>>{
>> return array_keys($GLOBALS);
>>}
>>var_dump(not_super(), array_keys($GLOBALS));
>>?>
>>
>>GLOBALS will be present in both arrays.
>>
>>Very odd behavior.
>>
>>Greg
>>
>>
>>Jim Lucas wrote:
>>
>>
>>
>>>actually, it does work and it does exist.
>>>
>>>Try using print_r() or
>>>
>>>print_r(array_keys($GLOBALS));
>>>
>>>
>>>and you will see an entry for GLOBALS
>>>
>>>mine is located at #13
>>>
>>>Jim Lucas
>>>
>>>----- Original Message -----
>>>From: "Greg Beaver" <greg@chiaraquartet.net>
>>>To: "Leif K-Brooks" <eurleif@buyer-brokerage.com>
>>>Cc: <php-general@lists.php.net>
>>>Sent: Friday, August 01, 2003 12:45 PM
>>>Subject: Re: [php] Re: Globals
>>>
>>>
>>>
>>>
>>>
>>>
>>>>Try this code:
>>>>
>>>><?php
>>>>function blah() {
>>>> var_dump($GLOBALS['GLOBALS']);
>>>>
>>>>}
>>>>
>>>>blah();
>>>>?>
>>>>
>>>>It appears that in a function scope, it doesn't. This is definitely a
>>>>bug, I'll post it if it hasn't already been noticed.
>>>>
>>>>Greg
>>>>
>>>>Leif K-Brooks wrote:
>>>>
>>>>
>>>>
>>>>
>>>>
>>>>>Greg Beaver wrote:
>>>>>
>>>>>
>>>>>
>>>>>
>>>>>
>>>>>>$GLOBALS does not contain a reference to itself
>>>>>>
>>>>>>
>>>>>>
>>>>>>
>>>>>Yes it does. I just ran the following, and I got "Greg is wrong."
>>>>>
>>>>><?php
>>>>>$foo = 'Greg is wrong.';
>>>>>echo $GLOBALS['GLOBALS']['GLOBALS']['GLOBALS']['foo'];
>>>>>?>
>>>>>
>>>>>
>>>>>
>>>>>
>>>>>
>>>>--
>>>>PHP General Mailing List (http://www.php.net/)
>>>>To unsubscribe, visit: http://www.php.net/unsub.php
>>>>
>>>>
>>>>
>>>>
>>>>
>>>>
>>>
>>>
>>>
>>>
>>>
>>>

>
>
>
>
>
>


Reply With Quote
  #6 (permalink)  
Old 08-01-2003
Jim Lucas
 
Posts: n/a
Default Re: [PHP] Re: Globals

I don't follow what you are trying to say here?

it is returning true, just as it should.

show me the results that you get by running this code.

here is mine

bool(true)
bool(true)


What is wrong about this?

Read my notes below

<?php
function not_super()
{
$v = isset($GLOBALS['GLOBALS']); // This will return TRUE or FALSE
var_dump($v); // This will echo the value of $v
// This again is going to be TRUE
or FALSE
// or it may show 1 or 0
return array_keys($GLOBALS); // because we are not echoing the
return below
// This will do nothing.
}
//var_dump(not_super(), array_keys($GLOBALS));
not_super(); // First pass through
not_super(); // Second pass through

What results are you expecting to get from this???
And why???

Jim


----- Original Message -----
From: "Greg Beaver" <greg@chiaraquartet.net>
To: "Jim Lucas" <phplist@zonedzero.net>
Cc: <php-general@lists.php.net>
Sent: Friday, August 01, 2003 2:43 PM
Subject: Re: [php] Re: Globals


> Jim,
>
> My bad, I posted the wrong code. Try this instead to see the bug:
>
> <?php
> function not_super()
> {
> var_dump(isset($GLOBALS['GLOBALS']));
> return array_keys($GLOBALS);
> }
> //var_dump(not_super(), array_keys($GLOBALS));
> not_super();
> not_super();
>
>
> ?>
>
> run this code, don't read it, you will see output of bool(false),
> bool(false)
>
> However, the following code works as expected!
>
> <?php
> function not_super()
> {
> return array_keys($GLOBALS);
> }
> var_dump(not_super(), array_keys($GLOBALS));
> ?>
>
> GLOBALS will be present in both arrays.
>
> Very odd behavior.
>
> Note that in PHP 5.0.0b1, the first function does work, so this is fixed
> somewhere in there.
>
> Greg
> --
> phpDocumentor
> http://www.phpdoc.org
>
> Jim Lucas wrote:
>
> >umm... That is what is going to happen when you follow your example.
> >
> >I think you are mistaken in what you are expecting...
> >
> >This line:
> > $a = array_keys($GLOBALS);
> >
> >will give you an array of values that match the keys of the $GLOBALS

array
> >
> >it won't copy the arrays.
> >
> >Try this.
> >
> ><PRE>
> ><?php
> >$a = array_keys($GLOBALS);
> >foreach($a AS $key => $value) {
> > echo $key."=>(".$value.")\n";
> >}
> >?>
> >
> >Jim Lucas
> >
> >----- Original Message -----
> >From: "Greg Beaver" <greg@chiaraquartet.net>
> >To: "Jim Lucas" <phplist@zonedzero.net>
> >Cc: "Leif K-Brooks" <eurleif@buyer-brokerage.com>;
> ><php-general@lists.php.net>
> >Sent: Friday, August 01, 2003 1:42 PM
> >Subject: Re: [php] Re: Globals
> >
> >
> >
> >
> >>Hi Jim,
> >>
> >>The code you posted is correct, I never contested that. Read carefully.
> >>
> >><?php
> >>function not_super()
> >>{
> >> var_dump(isset($GLOBALS['GLOBALS']));
> >> $a = array_keys($GLOBALS);
> >>
> >> var_dump(isset($a['GLOBALS']));
> >>}
> >>not_super();
> >>?>
> >>
> >>run this code, don't read it, you will see output of bool(false),
> >>bool(false)
> >>
> >>However, the following code works as expected!
> >>
> >><?php
> >>function not_super()
> >>{
> >> return array_keys($GLOBALS);
> >>}
> >>var_dump(not_super(), array_keys($GLOBALS));
> >>?>
> >>
> >>GLOBALS will be present in both arrays.
> >>
> >>Very odd behavior.
> >>
> >>Greg
> >>
> >>
> >>Jim Lucas wrote:
> >>
> >>
> >>
> >>>actually, it does work and it does exist.
> >>>
> >>>Try using print_r() or
> >>>
> >>>print_r(array_keys($GLOBALS));
> >>>
> >>>
> >>>and you will see an entry for GLOBALS
> >>>
> >>>mine is located at #13
> >>>
> >>>Jim Lucas
> >>>
> >>>----- Original Message -----
> >>>From: "Greg Beaver" <greg@chiaraquartet.net>
> >>>To: "Leif K-Brooks" <eurleif@buyer-brokerage.com>
> >>>Cc: <php-general@lists.php.net>
> >>>Sent: Friday, August 01, 2003 12:45 PM
> >>>Subject: Re: [php] Re: Globals
> >>>
> >>>
> >>>
> >>>
> >>>
> >>>
> >>>>Try this code:
> >>>>
> >>>><?php
> >>>>function blah() {
> >>>> var_dump($GLOBALS['GLOBALS']);
> >>>>
> >>>>}
> >>>>
> >>>>blah();
> >>>>?>
> >>>>
> >>>>It appears that in a function scope, it doesn't. This is definitely a
> >>>>bug, I'll post it if it hasn't already been noticed.
> >>>>
> >>>>Greg
> >>>>
> >>>>Leif K-Brooks wrote:
> >>>>
> >>>>
> >>>>
> >>>>
> >>>>
> >>>>>Greg Beaver wrote:
> >>>>>
> >>>>>
> >>>>>
> >>>>>
> >>>>>
> >>>>>>$GLOBALS does not contain a reference to itself
> >>>>>>
> >>>>>>
> >>>>>>
> >>>>>>
> >>>>>Yes it does. I just ran the following, and I got "Greg is wrong."
> >>>>>
> >>>>><?php
> >>>>>$foo = 'Greg is wrong.';
> >>>>>echo $GLOBALS['GLOBALS']['GLOBALS']['GLOBALS']['foo'];
> >>>>>?>
> >>>>>
> >>>>>
> >>>>>
> >>>>>
> >>>>>
> >>>>--
> >>>>PHP General Mailing List (http://www.php.net/)
> >>>>To unsubscribe, visit: http://www.php.net/unsub.php
> >>>>
> >>>>
> >>>>
> >>>>
> >>>>
> >>>>
> >>>
> >>>
> >>>
> >>>
> >>>
> >>>

> >
> >
> >
> >
> >
> >

>


Reply With Quote
  #7 (permalink)  
Old 08-02-2003
Jim Lucas
 
Posts: n/a
Default Re: [PHP] Re: Globals

I am running PHP 4.2.2 on Debian linux with apache 1.3.26

Jim
----- Original Message -----
From: Greg Beaver
To: Jim Lucas
Cc: php-general@lists.php.net
Sent: Friday, August 01, 2003 3:02 PM
Subject: Re: [php] Re: Globals


What version of PHP are you running? I have PHP 4.3.2 on windows XP here. I'm getting bool(false) bool(false) every time.

Greg

Jim Lucas wrote:

I don't follow what you are trying to say here?

it is returning true, just as it should.

show me the results that you get by running this code.

here is mine

bool(true)
bool(true)


What is wrong about this?

Read my notes below

<?php
function not_super()
{
$v = isset($GLOBALS['GLOBALS']); // This will return TRUE or FALSE
var_dump($v); // This will echo the value of $v
// This again is going to be TRUE
or FALSE
// or it may show 1 or 0
return array_keys($GLOBALS); // because we are not echoing the
return below
// This will do nothing.
}
//var_dump(not_super(), array_keys($GLOBALS));
not_super(); // First pass through
not_super(); // Second pass through

What results are you expecting to get from this???
And why???

Jim


----- Original Message -----
From: "Greg Beaver" <greg@chiaraquartet.net>
To: "Jim Lucas" <phplist@zonedzero.net>
Cc: <php-general@lists.php.net>
Sent: Friday, August 01, 2003 2:43 PM
Subject: Re: [php] Re: Globals



Jim,

My bad, I posted the wrong code. Try this instead to see the bug:

<?php
function not_super()
{
var_dump(isset($GLOBALS['GLOBALS']));
return array_keys($GLOBALS);
}
//var_dump(not_super(), array_keys($GLOBALS));
not_super();
not_super();


?>

run this code, don't read it, you will see output of bool(false),
bool(false)

However, the following code works as expected!

<?php
function not_super()
{
return array_keys($GLOBALS);
}
var_dump(not_super(), array_keys($GLOBALS));
?>

GLOBALS will be present in both arrays.

Very odd behavior.

Note that in PHP 5.0.0b1, the first function does work, so this is fixed
somewhere in there.

Greg
--
phpDocumentor
http://www.phpdoc.org

Jim Lucas wrote:


umm... That is what is going to happen when you follow your example.

I think you are mistaken in what you are expecting...

This line:
$a = array_keys($GLOBALS);

will give you an array of values that match the keys of the $GLOBALS

array

it won't copy the arrays.

Try this.

<PRE>
<?php
$a = array_keys($GLOBALS);
foreach($a AS $key => $value) {
echo $key."=>(".$value.")\n";
}
?>

Jim Lucas

----- Original Message -----
From: "Greg Beaver" <greg@chiaraquartet.net>
To: "Jim Lucas" <phplist@zonedzero.net>
Cc: "Leif K-Brooks" <eurleif@buyer-brokerage.com>;
<php-general@lists.php.net>
Sent: Friday, August 01, 2003 1:42 PM
Subject: Re: [php] Re: Globals





Hi Jim,

The code you posted is correct, I never contested that. Read carefully.

<?php
function not_super()
{
var_dump(isset($GLOBALS['GLOBALS']));
$a = array_keys($GLOBALS);

var_dump(isset($a['GLOBALS']));
}
not_super();
?>

run this code, don't read it, you will see output of bool(false),
bool(false)

However, the following code works as expected!

<?php
function not_super()
{
return array_keys($GLOBALS);
}
var_dump(not_super(), array_keys($GLOBALS));
?>

GLOBALS will be present in both arrays.

Very odd behavior.

Greg


Jim Lucas wrote:




actually, it does work and it does exist.

Try using print_r() or

print_r(array_keys($GLOBALS));


and you will see an entry for GLOBALS

mine is located at #13

Jim Lucas

----- Original Message -----
From: "Greg Beaver" <greg@chiaraquartet.net>
To: "Leif K-Brooks" <eurleif@buyer-brokerage.com>
Cc: <php-general@lists.php.net>
Sent: Friday, August 01, 2003 12:45 PM
Subject: Re: [php] Re: Globals







Try this code:

<?php
function blah() {
var_dump($GLOBALS['GLOBALS']);

}

blah();
?>

It appears that in a function scope, it doesn't. This is definitely a
bug, I'll post it if it hasn't already been noticed.

Greg

Leif K-Brooks wrote:






Greg Beaver wrote:






$GLOBALS does not contain a reference to itself





Yes it does. I just ran the following, and I got "Greg is wrong."

<?php
$foo = 'Greg is wrong.';
echo $GLOBALS['GLOBALS']['GLOBALS']['GLOBALS']['foo'];
?>






--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
























Reply With Quote
Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT +1. The time now is 03:02 AM.


Powered by vBulletin® Version 3.7.3
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO 3.0.0