Re: cannot access inc files from a function
Available news archives: comp.lang.tcl - comp.lang.python - comp.security.firewalls - sci.crypt - comp.lang.php - comp.lang.javascript
Google
 
Web news.hping.org


comp.lang.php archive

Re: cannot access inc files from a function

From: Giannis Vrentzos <gvre@gvre.gr>
Date: Fri Jul 29 2005 - 16:53:53 CEST

bettina@coaster.ch wrote:
> How should I define the global variable inside this function? What must
> be global is the lang.{$lang}.inc.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($myTechnik ?>
>

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

        if ( file_exists($incFile) )
                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}.inc.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}.inc.php";
if ( file_exists($incFile) )
                include ($incFile);
else
        return; // or any other action

// functions file
function technique($t) {
        global $strings;
        
        if (array_key_exists($t, $strings))
                    echo $strings[$t];

}
Received on Mon Oct 17 21:22:39 2005