Professional Web Applications Themes

Donít unset that variable you need globally - PHP Development

Dontí make my mistake. It is costly. Say you have defined a variable $var in your main script. Now in a function you access it using: global $var; But you want to set it to null inside the function. DO: $var = null; DONT DO: unset($var); Why? Because the 2nd way removes the variable from the name space, and it is no longer globally available. Cost me 10 hours.... -- Posted using the http://www.dbforumz.com interface, at author's request Articles individually checked for conformance to usenet standards Topic URL: http://www.dbforumz.com/PHP-Don-unset-variable-globally-ftopict237516.html Visit Topic URL to contact author (reg. req'd). Report abuse: http://www.dbforumz.com/eform.php?p=825331...

  1. #1

    Default Donít unset that variable you need globally

    Dontí make my mistake. It is costly.

    Say you have defined a variable $var in your main script.

    Now in a function you access it using:
    global $var;

    But you want to set it to null inside the function.
    DO: $var = null;
    DONT DO: unset($var);

    Why? Because the 2nd way removes the variable from the name space,
    and it is no longer globally available. Cost me 10 hours....

    --
    Posted using the http://www.dbforumz.com interface, at author's request
    Articles individually checked for conformance to usenet standards
    Topic URL: http://www.dbforumz.com/PHP-Don-unset-variable-globally-ftopict237516.html
    Visit Topic URL to contact author (reg. req'd). Report abuse: http://www.dbforumz.com/eform.php?p=825331
    steve Guest

  2. #2

    Default Re: Donít unset that variable you need globally

    steve wrote: 
    Thanks for the heads up, although I do remember reading something along
    these lines in the PHP manual.
    Exyle Guest

  3. #3

    Default Re: Don?t unset that variable you need globally

    steve wrote: 

    Even better -- don't use global variables.

    Berislav


    Berislav Guest

  4. #4

    Default Re: Re: Donít_unset_that_variable_you_n

    "" wrote: 
    > space, 
    > Thanks for the heads up, although I do remember reading
    > something along
    > these lines in the PHP manual.[/ref]

    You are welcome, and if you find that inf on php site, please post.

    --
    Posted using the http://www.dbforumz.com interface, at author's request
    Articles individually checked for conformance to usenet standards
    Topic URL: http://www.dbforumz.com/PHP-Don-unset-variable-globally-ftopict237516.html
    Visit Topic URL to contact author (reg. req'd). Report abuse: http://www.dbforumz.com/eform.php?p=825739
    steve Guest

  5. #5

    Default Re: Donít unset that variable you need globally

    "steve" <com> wrote in message
    news:com... 

    That's odd. The manual seems to disagree:

    "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.

    <?php
    function destroy_foo()
    {
    global $foo;
    unset($foo);
    }

    $foo = 'bar';
    destroy_foo();
    echo $foo; // prints "bar"
    ?>"
    - from http://fi.php.net/manual/en/function.unset.php

    Now I don't know what to believe..

    --
    Welcome to Usenet! Please leave tolerance, understanding
    and intelligence at the door. They aren't welcome here.
    eternal piste erection miuku gmail piste com


    Kimmo Guest

  6. #6

    Default Re: Don't unset that variable you need globally

    > Steve wrote: [/ref]

    Kimmo Laine wrote: 

    That's exactly what Steve said. He said that the variable is removed
    from current namespace (function namespace), not that it's totaly
    removed.

    "global" keyword is like setting local reference to global variable.
    When you call "unset", then you unset reference to that variable.
    To "unset" global variable from a function you'll have to
    "unset( $_GLOBAL['foo'] )".


    Hilarion
    Hilarion Guest

  7. #7

    Default Re: Don't_unset_that_variable_you_n

    > > steve wrote: [/ref][/ref]
     

    Read Kimmo Laine post. It contains the link and manual fragment
    which describes the case ("the local variable is destroyed").

    Hilarion
    Hilarion Guest

  8. #8

    Default Re: Don't unset that variable you need globally

    "Hilarion" <op.SMIECI.pl> wrote in message
    news:dadovd$8n8$onet.pl... [/ref]
    >
    > Kimmo Laine wrote: 
    >
    > That's exactly what Steve said. He said that the variable is removed
    > from current namespace (function namespace), not that it's totaly
    > removed.[/ref]


    I somehow thought he meant from the entire namespace, not from current...
    Okay, my bad. Now it all makes sense.

    --
    Welcome to Usenet! Please leave tolerance, understanding
    and intelligence at the door. They aren't welcome here.
    eternal piste erection miuku gmail piste com


    Kimmo Guest

  9. #9

    Default Re: Don't unset that variable you need globally


    "Kimmo Laine" <com> wrote in message
    news:DVtye.2045$news.jippii.net...
    | "Hilarion" <op.SMIECI.pl> wrote in message
    | news:dadovd$8n8$onet.pl...
    | >> Steve wrote:
    | >> > Say you have defined a variable $var in your main script.
    | >> >
    | >> > Now in a function you access it using:
    | >> > global $var;
    | >> >
    | >> > But you want to set it to null inside the function.
    | >> > DO: $var = null;
    | >> > DONT DO: unset($var);
    | >> >
    | >> > Why? Because the 2nd way removes the variable from the name space,
    | >> > and it is no longer globally available. Cost me 10 hours....
    | >
    | > Kimmo Laine wrote:
    | >> That's odd. The manual seems to disagree:
    | >>
    | >> "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.
    | >>
    | >> <?php
    | >> function destroy_foo()
    | >> {
    | >> global $foo;
    | >> unset($foo);
    | >> }
    | >>
    | >> $foo = 'bar';
    | >> destroy_foo();
    | >> echo $foo; // prints "bar"
    | >> ?>"
    | >> - from http://fi.php.net/manual/en/function.unset.php
    | >
    | > That's exactly what Steve said. He said that the variable is removed
    | > from current namespace (function namespace), not that it's totaly
    | > removed.
    |
    |
    | I somehow thought he meant from the entire namespace, not from current...
    | Okay, my bad. Now it all makes sense.

    hmmm...i don't believe you have mis-read anything. i read steve's op that
    same way you did *and* the problem he describes *does* conflict with the php
    manual. but then again, we may both be having problems with our reading
    comprehension. ;^)


    steve Guest

  10. #10

    Default Re: Don't unset that variable you need globally

    > hmmm...i don't believe you have mis-read anything. i read steve's op that 


    :)

    OK. The way I read it was:
     

    I suppose the above text was clear and requires no translation.

     

    The above means "you want to unset global variable $var / set it
    to null"

     

    "Because the 2nd way removes the variable from current (function) namespace"

     

    "You do not have acces to the global variable from current (function) scope."

     



    Hilarion

    PS.: The fact that English is not my native language is probably influencing
    the way that I read it.
    Hilarion Guest

  11. #11

    Default Re: Don?t unset that variable you need globally

    Berislav Lopac wrote: 
    >
    > Even better -- don't use global variables.[/ref]

    Easier said than done. There are times when globals are the best solution.
    There IS a reason they're available...

    --
    Tony Garcia
    Web Right! Development


    Tony Guest

  12. #12

    Default Re: Donít unset that variable you need globally

    On 5 Jul 2005 15:38:12 -0400, steve <com> wrote:
     

    Although the variable remains set that way - it's just null.

    To get rid of it completely: unset($GLOBALS['var']);

    --
    Andy Hassall / <co.uk> / <http://www.andyh.co.uk>
    <http://www.andyhsoftware.co.uk/space> Space: disk usage ysis tool
    Andy Guest

  13. #13

    Default Re: Don?t unset that variable you need globally

    In comp.lang.php steve <com> wrote: 

    $DEITY forbid that anyone should read the manual!

    But one could consider the global keyword to be extremely evil (changing
    scopes of variables is extremly yuck), use the superglobal $GLOBALS
    instead (note the unintuitive missing '_').

    FUP to comp.lang.php
    Daniel Guest

  14. #14

    Default Re: Don't unset that variable you need globally

    > "unset( $_GLOBAL['foo'] )".

    Should be:

    "unset( $GLOBALS['foo'] )"

    My mistake (I noticed it after reading post of Daniel Tryba).


    Hilarion


    Hilarion Guest

  15. #15

    Default Re: Don't unset that variable you need globally

    In comp.lang.php Hilarion <onet.pl> wrote: 
    > Should be:
    >
    > "unset( $GLOBALS['foo'] )"
    >
    > My mistake (I noticed it after reading post of Daniel Tryba).[/ref]

    Hehehe :)

    A good thing I missed your posting and essentially wrote the same as in
    a double blindfolded second opinion test :)

    Daniel Guest

  16. #16

    Default Re: Re: Don't unset that variable you need globally

    "" wrote: 
    > > Should be:
    > >
    > > "unset( $GLOBALS['foo'] )"
    > >
    > > My mistake (I noticed it after reading post of Daniel[/ref]
    > Tryba).
    >
    > Hehehe :)
    >
    > A good thing I missed your posting and essentially wrote the
    > same as in
    > a double blindfolded second opinion test :)[/ref]

    ok, so what is the difference between:

    unset($var)
    and
    $var = null;

    I know the first form removes the $var from name space, but is there
    any functions to check the difference. isset() responds the same way
    to both.

    --
    Posted using the http://www.dbforumz.com interface, at author's request
    Articles individually checked for conformance to usenet standards
    Topic URL: http://www.dbforumz.com/PHP-Don-unset-variable-globally-ftopict237516.html
    Visit Topic URL to contact author (reg. req'd). Report abuse: http://www.dbforumz.com/eform.php?p=826716
    steve Guest

  17. #17

    Default Re: Don't unset that variable you need globally

    In comp.lang.php steve <com> wrote: 

    http://php.net/isset says it all:

    "isset() will return FALSE if testing a variable that has been set to
    NULL."

    I have no idea why (and who decided that) a variable set to null is
    considered unset (and a reference to a variable with value null not
    raising a "Undefined variable" notice). IMHO it simply should return
    true since it is just plain inconsistent.

    Daniel Guest

  18. #18

    Default Re: Re: Don't unset that variable you need globally

    "" wrote: 
    > is there 
    > same way 
    >
    > http://php.net/isset says it all:
    >
    > "isset() will return FALSE if testing a variable that has been
    > set to
    > NULL."
    >
    > I have no idea why (and who decided that) a variable set to
    > null is
    > considered unset (and a reference to a variable with value
    > null not
    > raising a "Undefined variable" notice). IMHO it simply should
    > return
    > true since it is just plain inconsistent.[/ref]

    I am looking for something like defined(CONSTANT) but I guess not..

    --
    Posted using the http://www.dbforumz.com interface, at author's request
    Articles individually checked for conformance to usenet standards
    Topic URL: http://www.dbforumz.com/PHP-Don-unset-variable-globally-ftopict237516.html
    Visit Topic URL to contact author (reg. req'd). Report abuse: http://www.dbforumz.com/eform.php?p=826852
    steve Guest

  19. #19

    Default Re: Don?t unset that variable you need globally

    Tony wrote: 
    >
    > Easier said than done. There are times when globals are the best
    > solution. There IS a reason they're available...[/ref]

    You can (and should) always pass a global variable as a function parameter.
    There is no effective difference between:

    function foo($bar)
    {
    global $foobar;
    return $bar + $foobar;
    }

    and

    function foo($bar, &$foobar)
    {
    return $bar + $foobar;
    }

    The difference is that when you call the second example you have to pass one
    more argument, but that prevents the encapsulation of the function, making
    it reusable and independent of the outside world.

    Berislav


    Berislav Guest

  20. #20

    Default Re: Re: Don't unset that variable you need globally

    | I am looking for something like defined(CONSTANT) but I guess not..

    what?

    you're crossing my wires. are you trying to define a constant or work with
    variable scope? why not just post your code that's giving you fits. i'd be
    willing to bet that the problem is not with either constants or globals.
    but, that's just me.


    steve Guest

Page 1 of 2 12 LastLast

Similar Threads

  1. Any way to unset a variable?
    By trave801 webforumsuser@macromedia.com in forum Macromedia Flash Actionscript
    Replies: 9
    Last Post: January 25th, 05:18 AM
  2. storing values non-globally
    By Kevin Pfeiffer in forum PERL Beginners
    Replies: 1
    Last Post: October 5th, 11:26 PM
  3. #21569 [Com]: PHP can't set cookies (setcookie) after invoking an unset variable
    By ggoswami at sbcglobal dot net in forum PHP Development
    Replies: 0
    Last Post: September 15th, 12:13 AM
  4. Replies: 0
    Last Post: July 9th, 04:39 PM
  5. #22014 [Com]: html_entity_decode with unset variable results in segfault
    By msw at altexa dot com in forum PHP Development
    Replies: 0
    Last Post: July 9th, 03:18 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