Re: $GLOBALS example script on php.net

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



I am unable to provide a better definition that "user defined variables".
User defined variables are variables that are declared by the user. User
here means the creator (or maintainer of the script).

So for:

<?php
$a = 'A';
$b = 'B';
$c = 'C';

function globals() {
    $globals = $GLOBALS;
    print_r("Before...");
    print_r($globals);

    foreach (array(
        'GLOBALS',
        '_ENV',
        'HTTP_ENV_VARS',
        '_POST',
        'HTTP_POST_VARS',
        '_GET',
        'HTTP_GET_VARS',
        '_COOKIE',
        'HTTP_COOKIE_VARS',
        '_SERVER',
        'HTTP_SERVER_VARS',
        '_FILES',
        'HTTP_POST_FILES',
        '_REQUEST'
    ) as $var) {
        unset($globals[$var]);
    }
    print("<br />After...");
       print_r($globals);

    return $globals;
}

globals();
?>

You will get: Array( [a] = A [b] => B [c] => C ). $a, $b and $c are user
defined variables.


On Mon, Mar 7, 2011 at 7:54 AM, Ashim Kapoor <ashimkapoor@xxxxxxxxx> wrote:

>
>
> Unsetting doesn't leave user defined variables. Unsetting simply destroys
>> variables (or removes elements from an array, etc). There is nothing magic
>> or hidden in that script. I think the note meant exactly what it said: after
>> creating a local copy of the $GLOBALS array and removing super globals from
>> it, all that's left in it are user defined variables. And that's exactly
>> what gets returned from the function.
>
>
>
> This is a script vars.php
>
>
> <?php
> function globals() {
>     $globals = $GLOBALS;
>     print_r("Before...");
>     print_r($globals);
>
>     foreach (array(
>         'GLOBALS',
>         '_ENV',
>         'HTTP_ENV_VARS',
>         '_POST',
>         'HTTP_POST_VARS',
>         '_GET',
>         'HTTP_GET_VARS',
>         '_COOKIE',
>         'HTTP_COOKIE_VARS',
>         '_SERVER',
>         'HTTP_SERVER_VARS',
>         '_FILES',
>         'HTTP_POST_FILES',
>         '_REQUEST'
>     ) as $var) {
>         unset($globals[$var]);
>     }
>     print("<br />After...");
>        print_r($globals);
>
>     return $globals;
> }
>
> globals();
> ?>
>
> I called http://localhost/vars.php?a=1
>
> I get : -
>
> Before...Array ( [GLOBALS] => Array *RECURSION* [_POST] => Array ( ) [_GET]
> => Array ( [a] => 1 ) [_COOKIE] => Array ( ) [_FILES] => Array ( ) )
> After...Array ( )
>
> ALL the variables are UNSET. I have a user defined $_GET[a] but that goes
> away too.
>
> One second, what do you mean by user defined variables? Maybe I am lost in
> comprehension

[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux