Variable scope mystery

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

    Variable scope mystery

    Here is a nice problem. I have two scripts, one calling the other with via
    an include. The script that is included contains a variable, a function and
    a call to that function. The variable needs to be accessible to the
    function, so I declare it having a global scope. Then I execute the
    function, which should echo the value of the variable. Contrary to what I
    expected, the variable isn't there.

    The code of the 2 programs is:

    script1.php
    <?php
    function blah()
    {
    include("script 2.php");
    }

    blah();
    ?>

    script2.php
    <?php
    $foo = "bar";

    function blahblah()
    {
    global $foo;
    echo $foo;
    }

    blahblah();
    ?>

    I can't see anything wrong with the way I set this up, even more since a
    call to script2.php does exactly what it's suppossed to do: echo the value.
    What's going on here is beyond me. Anybody has a clue?

    Kind regards, Maarten


  • daemon

    #2
    Re: Variable scope mystery

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

    Maarten wrote:[color=blue]
    > Here is a nice problem. I have two scripts, one calling the other with via
    > an include. The script that is included contains a variable, a function and
    > a call to that function. The variable needs to be accessible to the
    > function, so I declare it having a global scope. Then I execute the
    > function, which should echo the value of the variable. Contrary to what I
    > expected, the variable isn't there.
    >
    > The code of the 2 programs is:
    >
    > script1.php
    > <?php
    > function blah()
    > {
    > include("script 2.php");
    > }
    >
    > blah();
    > ?>
    >
    > script2.php
    > <?php
    > $foo = "bar";
    >
    > function blahblah()
    > {
    > global $foo;
    > echo $foo;
    > }
    >
    > blahblah();
    > ?>
    >
    > I can't see anything wrong with the way I set this up, even more since a
    > call to script2.php does exactly what it's suppossed to do: echo the value.
    > What's going on here is beyond me. Anybody has a clue?
    >
    > Kind regards, Maarten
    >
    >[/color]

    I can... you can't expect to include another function within blah();

    To do it, use a class, and include your blahblah(); and just use blah();
    to reference to it within your class::function ();
    -----BEGIN PGP SIGNATURE-----
    Version: GnuPG v1.4.2 (MingW32)
    Comment: Using GnuPG with Thunderbird - http://enigmail.mozdev.org

    iD8DBQFDfmZh/WE0aXnOUiYRAmpQ AJ4gbRQBepMOTri ilvr82GryQuykTQ Cgk0TZ
    tZ637ZaemoIUKhM 4n4Frh2w=
    =f1n2
    -----END PGP SIGNATURE-----

    Comment

    • Oli Filth

      #3
      Re: Variable scope mystery

      daemon said the following on 18/11/2005 16:43:[color=blue]
      > -----BEGIN PGP SIGNED MESSAGE-----
      > Hash: SHA1
      >
      > Maarten wrote:
      >[color=green]
      >>Here is a nice problem. I have two scripts, one calling the other with via
      >>an include. The script that is included contains a variable, a function and
      >>a call to that function. The variable needs to be accessible to the
      >>function, so I declare it having a global scope. Then I execute the
      >>function, which should echo the value of the variable. Contrary to what I
      >>expected, the variable isn't there.
      >>
      >>The code of the 2 programs is:
      >>
      >>script1.php
      >><?php
      >>function blah()
      >>{
      >> include("script 2.php");
      >>}
      >>
      >>blah();
      >>?>
      >>
      >>script2.php
      >><?php
      >>$foo = "bar";
      >>
      >>function blahblah()
      >>{
      >> global $foo;
      >> echo $foo;
      >>}
      >>
      >>blahblah();
      >>?>
      >>
      >>I can't see anything wrong with the way I set this up, even more since a
      >>call to script2.php does exactly what it's suppossed to do: echo the value.
      >>What's going on here is beyond me. Anybody has a clue?
      >>
      >>Kind regards, Maarten
      >>
      >>[/color]
      >
      >
      > I can... you can't expect to include another function within blah();[/color]

      Yes you can.

      The problem is that by declaring $foo = "bar" in what intuitively looks
      like the global scope within script2.php, it's actually being declared
      in the scope of blah(), and therefore isn't a global variable, and
      therefore can't be accessed within blahblah().

      Change script2.php to:

      <?php
      global $foo;
      $foo = "bar";

      function blahblah()
      {
      global $foo;
      echo $foo;
      }

      blahblah();
      ?>



      --
      Oli

      Comment

      • Maarten

        #4
        Re: Variable scope mystery

        Thank you for replying, but you are mistaking. This for example works fine,
        even when the second function is included from an outside file.

        function blah()
        {
        function blahblah()
        {
        echo "foo";
        }
        blahblah();
        }
        blah();



        "daemon" <d43m0n@shaw.ca > wrote in message
        news:cxnff.5236 97$oW2.516262@p d7tw1no...[color=blue]
        > -----BEGIN PGP SIGNED MESSAGE-----
        > Hash: SHA1
        >
        > Maarten wrote:[color=green]
        >> Here is a nice problem. I have two scripts, one calling the other with
        >> via
        >> an include. The script that is included contains a variable, a function
        >> and
        >> a call to that function. The variable needs to be accessible to the
        >> function, so I declare it having a global scope. Then I execute the
        >> function, which should echo the value of the variable. Contrary to what I
        >> expected, the variable isn't there.
        >>
        >> The code of the 2 programs is:
        >>
        >> script1.php
        >> <?php
        >> function blah()
        >> {
        >> include("script 2.php");
        >> }
        >>
        >> blah();
        >> ?>
        >>
        >> script2.php
        >> <?php
        >> $foo = "bar";
        >>
        >> function blahblah()
        >> {
        >> global $foo;
        >> echo $foo;
        >> }
        >>
        >> blahblah();
        >> ?>
        >>
        >> I can't see anything wrong with the way I set this up, even more since a
        >> call to script2.php does exactly what it's suppossed to do: echo the
        >> value.
        >> What's going on here is beyond me. Anybody has a clue?
        >>
        >> Kind regards, Maarten
        >>
        >>[/color]
        >
        > I can... you can't expect to include another function within blah();
        >
        > To do it, use a class, and include your blahblah(); and just use blah();
        > to reference to it within your class::function ();
        > -----BEGIN PGP SIGNATURE-----
        > Version: GnuPG v1.4.2 (MingW32)
        > Comment: Using GnuPG with Thunderbird - http://enigmail.mozdev.org
        >
        > iD8DBQFDfmZh/WE0aXnOUiYRAmpQ AJ4gbRQBepMOTri ilvr82GryQuykTQ Cgk0TZ
        > tZ637ZaemoIUKhM 4n4Frh2w=
        > =f1n2
        > -----END PGP SIGNATURE-----[/color]


        Comment

        • Maarten

          #5
          Re: Variable scope mystery

          Yes, that's it! So, a variable declared inside a function isn't available
          inside nested function even while you are calling it in (global). You'll
          have to declare it global in the main function first. This can cause
          problems though, as there might be a variable with the same name outside the
          main function that you do not want to access. See what I mean? The solution
          is obvious of course, but sometimes you just don't know what variables live
          out there, do you?


          "Oli Filth" <catch@olifilth .co.uk> wrote in message
          news:YTnff.1224 2$fN5.7154@news fe6-gui.ntli.net...[color=blue]
          > daemon said the following on 18/11/2005 16:43:[color=green]
          >> -----BEGIN PGP SIGNED MESSAGE-----
          >> Hash: SHA1
          >>
          >> Maarten wrote:
          >>[color=darkred]
          >>>Here is a nice problem. I have two scripts, one calling the other with
          >>>via an include. The script that is included contains a variable, a
          >>>function and a call to that function. The variable needs to be accessible
          >>>to the function, so I declare it having a global scope. Then I execute
          >>>the function, which should echo the value of the variable. Contrary to
          >>>what I expected, the variable isn't there.
          >>>
          >>>The code of the 2 programs is:
          >>>
          >>>script1.ph p
          >>><?php
          >>>function blah()
          >>>{
          >>> include("script 2.php");
          >>>}
          >>>
          >>>blah();
          >>>?>
          >>>
          >>>script2.ph p
          >>><?php
          >>>$foo = "bar";
          >>>
          >>>function blahblah()
          >>>{
          >>> global $foo;
          >>> echo $foo;
          >>>}
          >>>
          >>>blahblah() ;
          >>>?>
          >>>
          >>>I can't see anything wrong with the way I set this up, even more since a
          >>>call to script2.php does exactly what it's suppossed to do: echo the
          >>>value. What's going on here is beyond me. Anybody has a clue?
          >>>
          >>>Kind regards, Maarten
          >>>[/color]
          >>
          >>
          >> I can... you can't expect to include another function within blah();[/color]
          >
          > Yes you can.
          >
          > The problem is that by declaring $foo = "bar" in what intuitively looks
          > like the global scope within script2.php, it's actually being declared in
          > the scope of blah(), and therefore isn't a global variable, and therefore
          > can't be accessed within blahblah().
          >
          > Change script2.php to:
          >
          > <?php
          > global $foo;
          > $foo = "bar";
          >
          > function blahblah()
          > {
          > global $foo;
          > echo $foo;
          > }
          >
          > blahblah();
          > ?>
          >
          >
          >
          > --
          > Oli[/color]


          Comment

          • Oli Filth

            #6
            Re: Variable scope mystery

            Maarten said the following on 18/11/2005 17:28:[color=blue]
            > Yes, that's it! So, a variable declared inside a function isn't available
            > inside nested function even while you are calling it in (global). You'll
            > have to declare it global in the main function first. This can cause
            > problems though, as there might be a variable with the same name outside the
            > main function that you do not want to access. See what I mean? The solution
            > is obvious of course, but sometimes you just don't know what variables live
            > out there, do you?
            >[/color]

            I think the solution is: Don't use global variables!

            Always explicitly pass required variables to functions.

            --
            Oli

            Comment

            • Maarten

              #7
              Re: Variable scope mystery

              What can I say? You are right of course. Thanks you very much for your help.

              Regards, Maarten

              "Oli Filth" <catch@olifilth .co.uk> wrote in message
              news:Lboff.1227 3$fN5.8401@news fe6-gui.ntli.net...[color=blue]
              > Maarten said the following on 18/11/2005 17:28:[color=green]
              >> Yes, that's it! So, a variable declared inside a function isn't available
              >> inside nested function even while you are calling it in (global). You'll
              >> have to declare it global in the main function first. This can cause
              >> problems though, as there might be a variable with the same name outside
              >> the main function that you do not want to access. See what I mean? The
              >> solution is obvious of course, but sometimes you just don't know what
              >> variables live out there, do you?
              >>[/color]
              >
              > I think the solution is: Don't use global variables!
              >
              > Always explicitly pass required variables to functions.
              >
              > --
              > Oli[/color]


              Comment

              Working...