Professional Web Applications Themes

[PHP] Globals - PHP Development

I can't speek about the first problem, but about the second one. You are not setting the variable $blah as a global variable. Try this instead $GLOBALS['blah'] = 'bob'; and that should work. You need to look into scope when refering to variables and functions. Jim Lucas ----- Original Message ----- From: "Chris Boget" <chriswild.net> To: "PHP General" <php-generallists.php.net> Sent: Friday, August 01, 2003 12:03 PM Subject: [PHP] Globals > I'm curious if someone could explain to me why this is occuring: > > function blah() { > //global $GLOBALS; > echo 'Globals: <pre>'; print_r( $GLOBALS ); echo '</pre>'; > ...

  1. #1

    Default Re: [PHP] Globals

    I can't speek about the first problem, but about the second one.

    You are not setting the variable $blah as a global variable.

    Try this instead

    $GLOBALS['blah'] = 'bob';

    and that should work.

    You need to look into scope when refering to variables and functions.

    Jim Lucas

    ----- Original Message -----
    From: "Chris Boget" <chriswild.net>
    To: "PHP General" <php-generallists.php.net>
    Sent: Friday, August 01, 2003 12:03 PM
    Subject: [PHP] Globals

    > I'm curious if someone could explain to me why this is occuring:
    >
    > 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?
    >
    > 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
    > the expected data inside the function?
    >
    > 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.
    >
    > Chris
    >
    >
    > --
    > PHP General Mailing List (http://www.php.net/)
    > To unsubscribe, visit: http://www.php.net/unsub.php
    >
    >
    Jim Lucas Guest

  2. #2

    Default Re: [PHP] Globals

    > I'm curious if someone could explain to me why this is occuring:
    >
    > 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 above is all I really need to know. The rest of my previous email
    was the result of a massive brain.
    Sorry about that...

    Chris

    Chris Boget Guest

  3. #3

    Default RE: [PHP] Globals

    [snip]
    $GLOBALS
    [/snip]

    From [url]http://us2.php.net/language.variables.scope[/url]

    "The $GLOBALS array is an associative array with the name of the global
    variable being the key and the contents of that variable being the value
    of the array element. Notice how $GLOBALS exists in any scope, this is
    because $GLOBALS is a superglobal."

    // Superglobals are available in any scope and do
    // not require 'global'. Superglobals are available
    // as of PHP 4.1.0

    HTH!
    Jay Blanchard Guest

  4. #4

    Default Re: [PHP] Globals

    Maybe it has to do with the fact that the $GLOBALS[] array has a copy of
    itself inside the parent.

    kinda weird but it looks like this

    $GLOBALS['key1'] = 'value1';
    $GLOBALS['key2'] = 'value2';
    $GLOBALS['GLOBALS']['key1'] = 'value1';
    $GLOBALS['GLOBALS']['key2'] = 'value2';

    don't ask me why, but it seems odd to me to do this. A little redundant if
    you ask me.

    Jim Lucas
    ----- Original Message -----
    From: "Chris Boget" <chriswild.net>
    To: "PHP General" <php-generallists.php.net>
    Sent: Friday, August 01, 2003 12:17 PM
    Subject: Re: [PHP] Globals

    >
    > > I'm curious if someone could explain to me why this is occuring:
    > >
    > > 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 above is all I really need to know. The rest of my previous email
    > was the result of a massive brain.
    > Sorry about that...
    >
    > Chris
    >
    >
    > --
    > PHP General Mailing List (http://www.php.net/)
    > To unsubscribe, visit: http://www.php.net/unsub.php
    >
    >
    Jim Lucas Guest

  5. #5

    Default Re: [PHP] Globals

    just to let you know, the $GLOBALS[] superglobals was around long before
    php 4.1.0

    Jim Lucas
    ----- Original Message -----
    From: "Jay Blanchard" <jay.blanchardniicommunications.com>
    To: "Chris Boget" <chriswild.net>; "PHP General"
    <php-generallists.php.net>
    Sent: Friday, August 01, 2003 12:18 PM
    Subject: RE: [PHP] Globals


    [snip]
    $GLOBALS
    [/snip]

    From http://us2.php.net/language.variables.scope

    "The $GLOBALS array is an associative array with the name of the global
    variable being the key and the contents of that variable being the value
    of the array element. Notice how $GLOBALS exists in any scope, this is
    because $GLOBALS is a superglobal."

    // Superglobals are available in any scope and do
    // not require 'global'. Superglobals are available
    // as of PHP 4.1.0

    HTH!

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



    Jim Lucas Guest

Similar Threads

  1. Replies: 0
    Last Post: October 2nd, 10:52 PM
  2. #25708 [WFx]: extract($GLOBALS, EXTR_REFS); can corrupt the $GLOBALS array.
    By cdragon at draconic dot com in forum PHP Development
    Replies: 0
    Last Post: October 1st, 01:39 AM
  3. Replies: 0
    Last Post: October 1st, 12:59 AM
  4. #25708 [NEW]: extract($GLOBALS, EXTR_REFS); can corrupt the $GLOBALS array.
    By cdragon at draconic dot com in forum PHP Development
    Replies: 0
    Last Post: September 30th, 10:25 PM
  5. Globals
    By Greg Beaver in forum PHP Development
    Replies: 6
    Last Post: August 1st, 10:10 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139