cannot access inc files from a function

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • bettina@coaster.ch

    cannot access inc files from a function

    I have the following piece of code (symplified)

    require("lang.{ $lang}.inc.php" ); // I include the inc. files
    ....
    function decodify($techn ique) { // functions that give me back the
    value in inc.file
    switch ($technique) {
    case 'C':
    echo $charcoal;
    break;
    case 'O':
    echo $oil;
    break;
    case 'A':
    echo $acrylic;
    break;
    }
    }
    .....
    Somewhere I call the function ....decodify($m yTechnique)

    But it doen't print the content from $charcoal, $oil or, etc.
    Do I have to declare the function global or something like that. In
    that case, how should I do it??
    Many thanks. Bettina

  • DXTrim

    #2
    Re: cannot access inc files from a function

    Hallo Bettina,

    What is the exact name of the file you are trying to include. Your construct
    is very weird, if the file is "lang.EN.inc.ph p" for example, try:

    require("lang.$ lang.inc.php");

    Also, could you post the code of that included file so we can see if the
    values are really there.

    Hope this helps,

    DXTrim


    <bettina@coaste r.ch> wrote in message
    news:1122642564 .772775.241500@ g49g2000cwa.goo glegroups.com.. .[color=blue]
    > I have the following piece of code (symplified)
    >
    > require("lang.{ $lang}.inc.php" ); // I include the inc. files
    > ...
    > function decodify($techn ique) { // functions that give me back the
    > value in inc.file
    > switch ($technique) {
    > case 'C':
    > echo $charcoal;
    > break;
    > case 'O':
    > echo $oil;
    > break;
    > case 'A':
    > echo $acrylic;
    > break;
    > }
    > }
    > ....
    > Somewhere I call the function ....decodify($m yTechnique)
    >
    > But it doen't print the content from $charcoal, $oil or, etc.
    > Do I have to declare the function global or something like that. In
    > that case, how should I do it??
    > Many thanks. Bettina
    >[/color]


    Comment

    • Giannis Vrentzos

      #3
      Re: cannot access inc files from a function

      bettina@coaster .ch wrote:[color=blue]
      > I have the following piece of code (symplified)
      >
      > require("lang.{ $lang}.inc.php" ); // I include the inc. files
      > ...
      > function decodify($techn ique) { // functions that give me back the
      > value in inc.file
      > switch ($technique) {
      > case 'C':
      > echo $charcoal;
      > break;
      > case 'O':
      > echo $oil;
      > break;
      > case 'A':
      > echo $acrylic;
      > break;
      > }
      > }
      > ....
      > Somewhere I call the function ....decodify($m yTechnique)
      >
      > But it doen't print the content from $charcoal, $oil or, etc.
      > Do I have to declare the function global or something like that. In
      > that case, how should I do it??
      > Many thanks. Bettina[/color]

      You cannot access a variable that is outside of a function unless you
      write global $var; inside the function. The other way is to include the
      lang file inside the function.

      Gvre

      Comment

      • bettina@coaster.ch

        #4
        Re: cannot access inc files from a function

        Hi DXTrim,

        the values are really there since I use the references outside the
        function and it work ok. I think it has something to do with what says
        Giannis regarding the global variable. I'll try that. Thank you.

        DXTrim schrieb:
        [color=blue]
        > Hallo Bettina,
        >
        > What is the exact name of the file you are trying to include. Your construct
        > is very weird, if the file is "lang.EN.inc.ph p" for example, try:
        >
        > require("lang.$ lang.inc.php");
        >
        > Also, could you post the code of that included file so we can see if the
        > values are really there.
        >
        > Hope this helps,
        >
        > DXTrim
        >
        >
        > <bettina@coaste r.ch> wrote in message
        > news:1122642564 .772775.241500@ g49g2000cwa.goo glegroups.com.. .[color=green]
        > > I have the following piece of code (symplified)
        > >
        > > require("lang.{ $lang}.inc.php" ); // I include the inc. files
        > > ...
        > > function decodify($techn ique) { // functions that give me back the
        > > value in inc.file
        > > switch ($technique) {
        > > case 'C':
        > > echo $charcoal;
        > > break;
        > > case 'O':
        > > echo $oil;
        > > break;
        > > case 'A':
        > > echo $acrylic;
        > > break;
        > > }
        > > }
        > > ....
        > > Somewhere I call the function ....decodify($m yTechnique)
        > >
        > > But it doen't print the content from $charcoal, $oil or, etc.
        > > Do I have to declare the function global or something like that. In
        > > that case, how should I do it??
        > > Many thanks. Bettina
        > >[/color][/color]

        Comment

        • bettina@coaster.ch

          #5
          Re: cannot access inc files from a function

          How should I define the global variable inside this function? What must
          be global is the lang.{$lang}.in c.php so that it can be read inside the
          function, isn't it?. I tried including the lang file in the function
          but I receive an error.

          require("lang.{ $lang}.inc.php" );
          function technique($t) {
          switch ($t) {
          case 'C':
          echo $charcoal;
          break;
          case 'O':
          echo $oil;
          break;
          case 'A':
          echo $acrylic;
          break;
          }
          }

          <? technique($myTe chnik ?>

          Comment

          • jamen

            #6
            Re: cannot access inc files from a function

            bettina@coaster .ch wrote:[color=blue]
            > How should I define the global variable inside this function? What must
            > be global is the lang.{$lang}.in c.php so that it can be read inside the
            > function[/color]
            [color=blue]
            > require("lang.{ $lang}.inc.php" );
            > function technique($t) {
            > switch ($t) {
            > case 'C':
            > echo $charcoal;
            >...[/color]

            $charcoal is local to your function. You can do it in two ways..

            1.
            function technique($t) {
            global $charcoal;

            switch ($t) {
            case 'C':
            echo $charcoal;



            2.
            function technique($t) {
            switch ($t) {
            case 'C':
            echo $GLOBAL['charcoal'];

            Comment

            • Giannis Vrentzos

              #7
              Re: cannot access inc files from a function

              bettina@coaster .ch wrote:[color=blue]
              > How should I define the global variable inside this function? What must
              > be global is the lang.{$lang}.in c.php so that it can be read inside the
              > function, isn't it?. I tried including the lang file in the function
              > but I receive an error.
              >
              > require("lang.{ $lang}.inc.php" );
              > function technique($t) {
              > switch ($t) {
              > case 'C':
              > echo $charcoal;
              > break;
              > case 'O':
              > echo $oil;
              > break;
              > case 'A':
              > echo $acrylic;
              > break;
              > }
              > }
              >
              > <? technique($myTe chnik ?>
              >[/color]

              function technique($t) {
              global $lang; // here we are using the global var $lang
              $incFile = "lang.{$lang}.i nc.php";

              if ( file_exists($in cFile) )
              include ($incFile);
              else
              return;

              switch ($t) {
              case 'C':
              echo $charcoal;
              break;
              case 'O':
              echo $oil;
              break;
              case 'A':
              echo $acrylic;
              break;
              }
              }

              Gvre

              ps. To OP. You can put all strings in file "lang.{$lang}.i nc.php" in an
              array and access it like this. If you do it like this you don't have to
              change the function technique($t) every time you put a new var in lang file.

              // vars file
              $strings = array (
              "C" => "charcoal",
              "O" => "oil",
              "A" => "acrylic"
              );


              // index ? file
              $incFile = "lang.{$lang}.i nc.php";
              if ( file_exists($in cFile) )
              include ($incFile);
              else
              return; // or any other action



              // functions file
              function technique($t) {
              global $strings;

              if (array_key_exis ts($t, $strings))
              echo $strings[$t];

              }

              Comment

              • bettina@coaster.ch

                #8
                Re: cannot access inc files from a function

                Thank you, the first option works ok!

                Comment

                • bettina@coaster.ch

                  #9
                  Re: cannot access inc files from a function

                  Hallo Giannis, that of putting everything in my lang file so that I
                  don't have to change the function every time I add a new variable works
                  fine! Thank you for the tip!

                  Giannis Vrentzos schrieb:
                  [color=blue]
                  > bettina@coaster .ch wrote:[color=green]
                  > > How should I define the global variable inside this function? What must
                  > > be global is the lang.{$lang}.in c.php so that it can be read inside the
                  > > function, isn't it?. I tried including the lang file in the function
                  > > but I receive an error.
                  > >
                  > > require("lang.{ $lang}.inc.php" );
                  > > function technique($t) {
                  > > switch ($t) {
                  > > case 'C':
                  > > echo $charcoal;
                  > > break;
                  > > case 'O':
                  > > echo $oil;
                  > > break;
                  > > case 'A':
                  > > echo $acrylic;
                  > > break;
                  > > }
                  > > }
                  > >
                  > > <? technique($myTe chnik ?>
                  > >[/color]
                  >
                  > function technique($t) {
                  > global $lang; // here we are using the global var $lang
                  > $incFile = "lang.{$lang}.i nc.php";
                  >
                  > if ( file_exists($in cFile) )
                  > include ($incFile);
                  > else
                  > return;
                  >
                  > switch ($t) {
                  > case 'C':
                  > echo $charcoal;
                  > break;
                  > case 'O':
                  > echo $oil;
                  > break;
                  > case 'A':
                  > echo $acrylic;
                  > break;
                  > }
                  > }
                  >
                  > Gvre
                  >
                  > ps. To OP. You can put all strings in file "lang.{$lang}.i nc.php" in an
                  > array and access it like this. If you do it like this you don't have to
                  > change the function technique($t) every time you put a new var in lang file.
                  >
                  > // vars file
                  > $strings = array (
                  > "C" => "charcoal",
                  > "O" => "oil",
                  > "A" => "acrylic"
                  > );
                  >
                  >
                  > // index ? file
                  > $incFile = "lang.{$lang}.i nc.php";
                  > if ( file_exists($in cFile) )
                  > include ($incFile);
                  > else
                  > return; // or any other action
                  >
                  >
                  >
                  > // functions file
                  > function technique($t) {
                  > global $strings;
                  >
                  > if (array_key_exis ts($t, $strings))
                  > echo $strings[$t];
                  >
                  > }[/color]

                  Comment

                  • Jasen Betts

                    #10
                    Re: cannot access inc files from a function

                    > But it doen't print the content from $charcoal, $oil or, etc.[color=blue]
                    > Do I have to declare the function global or something like that. In
                    > that case, how should I do it??
                    > Many thanks. Bettina
                    >[/color]

                    to combine some of the ideas in this thread...
                    replace the whole case block with:

                    echo $GLOBAL[array('C'=>'cha rcoal','O'=>'oi l','A'=>'acryli c')[$technique]];


                    Bye.
                    Jasen

                    Comment

                    Working...