Jump to content

Recommended Posts

Hi all,

 

I've encountered a curious problem, the variable $langs is defined in an include file, and used by a function defined in the same include file?  But it doesnt seem to recognize the variable...

 

index.php :

    include("lang_utils.inc");
    $lang=detect_lang();
    ...

 

lang_utils.inc :

$langs =  array (
    "fr" => "français",
    "en" => "English",
    "es" => "espanol",
    "pt" => "portugese"
);

function detect_lang(){
    ...
    if (!array_key_exists($lang, $langs)){
        $lang='en';
    }
    ...
    return $lang;
}

 

Straight forward right?  So i was surprised when a user reported this warning visible on the site

 

Warning: array_key_exists() [function.array-key-exists]: The second argument should be either an array or an object in .../lang_utils.inc on line 42

 

line 42 is the "if (!array_key_exists($lang, $langs)){" copied above.  So according to the warning, $langs didnt get defined.  :confused:

 

It might be worth noting tnat not everybody gets this warning...  it was encountered at least once with firefox on ubuntu. 

 

Anybody see where it goes wrong?

 

thanks

 

 

 

 

Where is $lang being set:

 

function detect_lang(){

    ...

    if (!array_key_exists($lang, $langs)){

        $lang='en';

    }

    ...

    return $lang;

}

 

You're not passing it into the function as a parameter, so unless something in ... sets it, then it's not defined within the scope of the function

Where is $lang being set:

 

You're not passing it into the function as a parameter, so unless something in ... sets it, then it's not defined within the scope of the function

 

yep, the "..." sets it.  i probably should have left it in even if its a little sloppy.  Anyway here's the whole function:

function detect_lang(){
   /**
    * Detect and include language, 
    *   returns variable $lang
    */
    $lang='';
    
    // take care of lang set by the user overide              
    if(isset($_POST["lang"])) {
        $_SESSION["lang"] = $_POST["lang"]; 
    }
    
    // then see if the language has already been determined
    if(isset($_SESSION["lang"])) {
        $lang = $_SESSION["lang"];
    } else {
        // try to guess the lang
        $accept_language = getenv('HTTP_ACCEPT_LANGUAGE');
        if (isset($accept_language)) {
            $lang = substr($accept_language, 0, 2);
            // note that firefox gives second and third choice lang info...
        
            if (!array_key_exists($lang, $langs)){
                $lang='en';
            }
        } else {
            $lang = $lang_default;
        }
        $_SESSION["lang"] = $lang;
    }
    return $lang;
}

 

it should work i reckon, and does...  with a warning§

 

*head just exploded*. Um....did you define a variable to equal a function that checks against that variable that calls that function that defines that variable that calls that function that checks that variable.............

 

whao, slow down my head's exploding too!  its not supposed to be that complicated.  :) 

 

I just include a file that does two things, it set the array $langs and defines a function detect_lang().

 

The variable $langs should be visible inside detect_lang() but i get a warning when i try to use it. 

the snippet

 

if (!array_key_exists($lang, $langs)){

                $lang='en';

            }

seems to always get executed.  The warning says that $langs is empty,  so array_key_exists returns FALSE and since its NOTted, the parser enters the if...

 

 

The array $langs isn't in function scope, only in global scope.

 

include the line:

global $langs

at the beginning of the function code,

 

okay, i figured $langs would be in the scope.    This means that in general :

$x=2;
function f() {
   $x+=1;
   return $x;
}

f should return 1 and x should be 2.  I tested it and yep! 

 

On the other hand with global

$x=2;
function f() {
   global $x;
   $x+=1;
   return $x;
}

and f returns 3, and x is 3 afterward.  perfect i get it now  ;D Thanks!!

 

This means that in general :

$x=2;
function f() {
   $x+=1;
   return $x;
}

f should return 1 and x should be 2.  I tested it and yep!

 

That is an erroneous operation. You are not allowed to manipulate or otherwise use uninitialized variables. It'll result in an E_NOTICE type error.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.