Don’t unset that variable you need globally

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • steve

    #1

    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-unse...ict237516.html
    Visit Topic URL to contact author (reg. req'd). Report abuse: http://www.dbforumz.com/eform.php?p=825331
  • Exyle

    #2
    Re: Don’t unset that variable you need globally

    steve wrote:[color=blue]
    > 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....
    >[/color]
    Thanks for the heads up, although I do remember reading something along
    these lines in the PHP manual.

    Comment

    • Berislav Lopac

      #3
      Re: Don?t unset that variable you need globally

      steve wrote:[color=blue]
      > 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....[/color]

      Even better -- don't use global variables.

      Berislav


      Comment

      • steve

        #4
        Re: Re: Don’t_unset_tha t_variable_you_ n

        "" wrote:[color=blue]
        > steve wrote:[color=green]
        > > 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[/color]
        > space,[color=green]
        > > and it is no longer globally available. Cost me 10 hours....
        > >[/color]
        > Thanks for the heads up, although I do remember reading
        > something along
        > these lines in the PHP manual.[/color]

        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-unse...ict237516.html
        Visit Topic URL to contact author (reg. req'd). Report abuse: http://www.dbforumz.com/eform.php?p=825739

        Comment

        • Kimmo Laine

          #5
          Re: Don’t unset that variable you need globally

          "steve" <UseLinkToEmail @dbForumz.com> wrote in message
          news:4_825331_3 a94f0381a978858 84a25578147e907 3@dbforumz.com. ..[color=blue]
          > 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....[/color]

          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


          Comment

          • Hilarion

            #6
            Re: Don't unset that variable you need globally

            > Steve wrote:[color=blue][color=green]
            > > 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....[/color][/color]

            Kimmo Laine wrote:[color=blue]
            > 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[/color]

            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

            Comment

            • Hilarion

              #7
              Re: Don't_unset_tha t_variable_you_ n

              > > steve wrote:[color=blue][color=green][color=darkred]
              > > > 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....[/color][/color][/color]
              [color=blue]
              > [...] if you find that inf on php site, please post.[/color]

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

              Hilarion

              Comment

              • Kimmo Laine

                #8
                Re: Don't unset that variable you need globally

                "Hilarion" <hilarion@SPAM. op.SMIECI.pl> wrote in message
                news:dadovd$8n8 $1@news.onet.pl ...[color=blue][color=green]
                >> Steve wrote:[color=darkred]
                >> > 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....[/color][/color]
                >
                > Kimmo Laine wrote:[color=green]
                >> 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[/color]
                >
                > That's exactly what Steve said. He said that the variable is removed
                > from current namespace (function namespace), not that it's totaly
                > removed.[/color]


                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


                Comment

                • steve

                  #9
                  Re: Don't unset that variable you need globally


                  "Kimmo Laine" <eternal.erecti onN05P@Mgmail.c om> wrote in message
                  news:DVtye.2045 $455.1532@reade r1.news.jippii. net...
                  | "Hilarion" <hilarion@SPAM. op.SMIECI.pl> wrote in message
                  | news:dadovd$8n8 $1@news.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. ;^)


                  Comment

                  • Hilarion

                    #10
                    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[color=blue]
                    > 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. ;^)[/color]


                    :)

                    OK. The way I read it was:
                    [color=blue]
                    > Say you have defined a variable $var in your main script.
                    >
                    > Now in a function you access it using:
                    > global $var;[/color]

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

                    [color=blue]
                    > But you want to set it to null inside the function.[/color]

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

                    [color=blue]
                    > DO: $var = null;
                    > DONT DO: unset($var);
                    >
                    > Why? Because the 2nd way removes the variable from the name space,[/color]

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

                    [color=blue]
                    > and it is no longer globally available.[/color]

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

                    [color=blue]
                    > Cost me 10 hours....[/color]



                    Hilarion

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

                    Comment

                    • Tony

                      #11
                      Re: Don?t unset that variable you need globally

                      Berislav Lopac wrote:[color=blue]
                      > steve wrote:[color=green]
                      >> 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....[/color]
                      >
                      > Even better -- don't use global variables.[/color]

                      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


                      Comment

                      • Andy Hassall

                        #12
                        Re: Don’t unset that variable you need globally

                        On 5 Jul 2005 15:38:12 -0400, steve <UseLinkToEmail @dbForumz.com> wrote:
                        [color=blue]
                        >That was my point, sorry for causing confusion. Thanks for reference.
                        >
                        >
                        >So unset does not work inside a function for global vars- it only
                        >destroys local var.
                        >
                        >The workaround is to say
                        >$var = null;
                        >inside functions.[/color]

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

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

                        --
                        Andy Hassall / <andy@andyh.co. uk> / <http://www.andyh.co.uk >
                        <http://www.andyhsoftwa re.co.uk/space> Space: disk usage analysis tool

                        Comment

                        • Daniel Tryba

                          #13
                          Re: Don?t unset that variable you need globally

                          In comp.lang.php steve <UseLinkToEmail @dbforumz.com> wrote:[color=blue]
                          > This is to my counter-intuitive, and unless you read the manual, one
                          > would not know.[/color]

                          $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

                          Comment

                          • Hilarion

                            #14
                            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


                            Comment

                            • Daniel Tryba

                              #15
                              Re: Don't unset that variable you need globally

                              In comp.lang.php Hilarion <m0rt_nospam_@n ospam_bez_spamu _itepe_poczta.o net.pl> wrote:[color=blue][color=green]
                              >> "unset( $_GLOBAL['foo'] )".[/color]
                              > Should be:
                              >
                              > "unset( $GLOBALS['foo'] )"
                              >
                              > My mistake (I noticed it after reading post of Daniel Tryba).[/color]

                              Hehehe :)

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

                              Comment

                              Working...