solariaman Posted August 20, 2009 Share Posted August 20, 2009 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. 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 Quote Link to comment https://forums.phpfreaks.com/topic/171142-solved-include-warning-maybe-namespace-issues/ Share on other sites More sharing options...
Mark Baker Posted August 20, 2009 Share Posted August 20, 2009 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 Quote Link to comment https://forums.phpfreaks.com/topic/171142-solved-include-warning-maybe-namespace-issues/#findComment-902523 Share on other sites More sharing options...
jonsjava Posted August 20, 2009 Share Posted August 20, 2009 *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............. Quote Link to comment https://forums.phpfreaks.com/topic/171142-solved-include-warning-maybe-namespace-issues/#findComment-902525 Share on other sites More sharing options...
solariaman Posted August 21, 2009 Author Share Posted August 21, 2009 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§ Quote Link to comment https://forums.phpfreaks.com/topic/171142-solved-include-warning-maybe-namespace-issues/#findComment-903130 Share on other sites More sharing options...
solariaman Posted August 21, 2009 Author Share Posted August 21, 2009 *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... Quote Link to comment https://forums.phpfreaks.com/topic/171142-solved-include-warning-maybe-namespace-issues/#findComment-903142 Share on other sites More sharing options...
Daniel0 Posted August 21, 2009 Share Posted August 21, 2009 Can't you just pass the variables by argument? Quote Link to comment https://forums.phpfreaks.com/topic/171142-solved-include-warning-maybe-namespace-issues/#findComment-903145 Share on other sites More sharing options...
Mark Baker Posted August 21, 2009 Share Posted August 21, 2009 The array $langs isn't in function scope, only in global scope. Either include the line: global $langs at the beginning of the function code, of pass the $langs array to the function as a parameter Quote Link to comment https://forums.phpfreaks.com/topic/171142-solved-include-warning-maybe-namespace-issues/#findComment-903187 Share on other sites More sharing options...
solariaman Posted August 21, 2009 Author Share Posted August 21, 2009 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 Thanks!! Quote Link to comment https://forums.phpfreaks.com/topic/171142-solved-include-warning-maybe-namespace-issues/#findComment-903388 Share on other sites More sharing options...
Daniel0 Posted August 21, 2009 Share Posted August 21, 2009 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. Quote Link to comment https://forums.phpfreaks.com/topic/171142-solved-include-warning-maybe-namespace-issues/#findComment-903404 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.