how do I make a variable global

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

    how do I make a variable global

    Hi Folk

    Consider this:

    ---foo1.php
    $myvar = 10;

    --- foo2.php
    require_once("f oo1.php");

    function funky {
    echo $myvar;
    }

    from my experience, funky does not print 10. How do I make $myvar "global"
    (not sure if this is the right word) so that it shows everywhere.


  • Steve

    #2
    Re: how do I make a variable global

    On Sun, 08 Jan 2006 10:33:00 +1300, windandwaves wrote:
    [color=blue]
    > Hi Folk
    >
    > Consider this:
    >
    > ---foo1.php
    > $myvar = 10;
    >
    > --- foo2.php
    > require_once("f oo1.php");
    >
    > function funky {
    > echo $myvar;
    > }
    >
    > from my experience, funky does not print 10. How do I make $myvar "global"
    > (not sure if this is the right word) so that it shows everywhere.[/color]

    A search on global might have helped (:
    <?

    $myvar = 10;

    function funky ()
    {
    global $myvar;
    echo $myvar;
    }

    funky ();
    ?>

    Comment

    • windandwaves

      #3
      Re: how do I make a variable global

      Steve wrote:[color=blue]
      > On Sun, 08 Jan 2006 10:33:00 +1300, windandwaves wrote:
      >[color=green]
      >> Hi Folk
      >>
      >> Consider this:
      >>
      >> ---foo1.php
      >> $myvar = 10;
      >>
      >> --- foo2.php
      >> require_once("f oo1.php");
      >>
      >> function funky {
      >> echo $myvar;
      >> }
      >>
      >> from my experience, funky does not print 10. How do I make $myvar
      >> "global" (not sure if this is the right word) so that it shows
      >> everywhere.[/color]
      >
      > A search on global might have helped (:
      > <?
      >
      > $myvar = 10;
      >
      > function funky ()
      > {
      > global $myvar;
      > echo $myvar;
      > }
      >
      > funky ();[/color]

      Thanks Steve,

      Sorry, yes, I did do the search on global, but I never really understood it
      very well. Thank you again.


      Comment

      • Iván Sánchez Ortega

        #4
        Re: how do I make a variable global

        -----BEGIN PGP SIGNED MESSAGE-----
        Hash: SHA1

        windandwaves wrote:
        [color=blue]
        > function funky {
        > echo $myvar;
        > }[/color]

        function funky {
        echo $GLOBALS['myvar'];
        }


        But, as you may already know, global variables are the dirty and bad way of
        doing stuff... reconsider passing parameters.

        - --
        - ----------------------------------
        Iván Sánchez Ortega -i-punto-sanchez--arroba-mirame-punto-net


        Proudly running Debian Linux with 2.6.12-1-686 kernel, KDE3.5.0, and PHP
        5.0.5-3 generating this signature.
        Uptime: 23:44:25 up 2 days, 7:35, 1 user, load average: 0.88, 0.33, 0.15

        -----BEGIN PGP SIGNATURE-----
        Version: GnuPG v1.4.2 (GNU/Linux)

        iD8DBQFDwESJ3jc Q2mg3Pc8RAr67AJ 9cB5iBYmJBevprw xx/olPz/AyfywCeM3iN
        y5AFqom7gd0HFFU Yr1kih2Q=
        =8rVb
        -----END PGP SIGNATURE-----

        Comment

        • cliff smith

          #5
          Re: how do I make a variable global

          windandwaves wrote:[color=blue]
          > Hi Folk
          >
          > Consider this:
          >
          > ---foo1.php
          > $myvar = 10;
          >
          > --- foo2.php
          > require_once("f oo1.php");
          >
          > function funky {
          > echo $myvar;
          > }
          >
          > from my experience, funky does not print 10. How do I make $myvar "global"
          > (not sure if this is the right word) so that it shows everywhere.
          >
          >[/color]

          Variables outside a function are not directly available inside a
          function. This is by design.
          Ways to make the function "funky" work.

          function funky() {
          echo $GLOBALS['myvar'];
          }
          funky();

          OR better, pass the variable to funky -

          function funky( $func_var ) {
          echo $func_var;
          }
          funky($myvar);

          Hope this helps,
          Cliff.

          Comment

          • windandwaves

            #6
            Re: how do I make a variable global

            Iván Sánchez Ortega wrote:[color=blue]
            > But, as you may already know, global variables are the dirty and bad
            > way of doing stuff... reconsider passing parameters.[/color]

            Yes, I know, but the variable I am using is a variable that should really be
            a define("myvar", "xxx");

            The reason i am using a variable is that these are easier to customise. I
            have file with standard values and then a config file that overrides any
            variable that should be customised for a particular application. Defined
            constants can only be defined once.

            Hope that makes sense


            Nicolaas


            Comment

            • Chung Leong

              #7
              Re: how do I make a variable global

              Yeah, down with globalization!

              Comment

              • Iván Sánchez Ortega

                #8
                Re: how do I make a variable global

                -----BEGIN PGP SIGNED MESSAGE-----
                Hash: SHA1

                windandwaves wrote:
                [color=blue]
                > The reason i am using a variable is that these are easier to customise. I
                > have file with standard values and then a config file that overrides any
                > variable that should be customised for a particular application. Defined
                > constants can only be defined once.
                >
                > Hope that makes sense[/color]

                No. Using global variables for configuration directives do not make sense.
                Ever. Using a singleton design pattern and static methods do, however.

                It's not about working or not-working code: it's about modular or
                non-modular code. Please keep all your config directives apart of the rest
                of the variables. It may save you from some headaches.

                - --
                - ----------------------------------
                Iván Sánchez Ortega -i-punto-sanchez--arroba-mirame-punto-net

                Trying to make bits uncopyable is like trying to make water not wet.
                -- Bruce Schneier
                -----BEGIN PGP SIGNATURE-----
                Version: GnuPG v1.4.2 (GNU/Linux)

                iD8DBQFDwiTQ3jc Q2mg3Pc8RAkJTAJ 46kyDh9QeLZFrUQ kC3A9AzHBJ8BACf YRL7
                4/A4xwjBWwh5fAiF0 HvYvT8=
                =d8jB
                -----END PGP SIGNATURE-----

                Comment

                • Dana Cartwright

                  #9
                  Re: how do I make a variable global

                  "windandwav es" <winandwaves@co ldmail.com> wrote in message
                  news:_3iwf.1354 1$vH5.701435@ne ws.xtra.co.nz.. .[color=blue]
                  > The reason i am using a variable is that these are easier to customise. I
                  > have file with standard values and then a config file that overrides any
                  > variable that should be customised for a particular application. Defined
                  > constants can only be defined once.
                  >[/color]

                  While you can't DEFINE something more than once, you can test to see if
                  something is already defined, and this enables you can use a system of
                  overrides.

                  I use two files, one containing overriding defines, and one containing the
                  default or factory values.

                  Let's say you want to override the value of 'FOO'. In your override file,
                  you say:

                  define( 'FOO' , 'myFoo' );

                  In the main configuration file, 'foo' is defined like this:

                  if ( !defined( 'FOO' ) )
                  define( 'FOO' , 'bar' );

                  which only defines 'FOO' if it's not already defined in the override file.

                  You have to INCLUDE your overriding defines file first.



                  Comment

                  • windandwaves

                    #10
                    Re: how do I make a variable global

                    Dana Cartwright wrote:[color=blue]
                    > "windandwav es" <winandwaves@co ldmail.com> wrote in message
                    > news:_3iwf.1354 1$vH5.701435@ne ws.xtra.co.nz.. .[color=green]
                    >> The reason i am using a variable is that these are easier to
                    >> customise. I have file with standard values and then a config file
                    >> that overrides any variable that should be customised for a
                    >> particular application. Defined constants can only be defined once.
                    >>[/color]
                    >
                    > While you can't DEFINE something more than once, you can test to see
                    > if something is already defined, and this enables you can use a
                    > system of overrides.
                    >
                    > I use two files, one containing overriding defines, and one
                    > containing the default or factory values.
                    >
                    > Let's say you want to override the value of 'FOO'. In your override
                    > file, you say:
                    >
                    > define( 'FOO' , 'myFoo' );
                    >
                    > In the main configuration file, 'foo' is defined like this:
                    >
                    > if ( !defined( 'FOO' ) )
                    > define( 'FOO' , 'bar' );
                    >
                    > which only defines 'FOO' if it's not already defined in the override
                    > file.
                    > You have to INCLUDE your overriding defines file first.[/color]

                    Hmm, thank you for that. That is a great idea. I actually use this concept
                    now, and I dont even think you need to add the if ( !defined( 'FOO' ) )
                    bit because, once defined, you can not redefine items.


                    Comment

                    Working...