RobinTibbs Posted January 14, 2007 Share Posted January 14, 2007 writing myself a small browser/Os id system and have run into an issue. here goesheres the file (info_cap.php)[code]/* DECLARATIONS */$USER_AGENT = $_SERVER['HTTP_USER_AGENT'];$browserInfo = array( "user_agent" => "", "browser_name" => "", "browser_version" => "", "os_name" => "", "os_version" => "",);/* FUNCTIONS */function RB_browserID($agentstring) { $browserInfo['user_agent'] = $agentstring; if(eregi("Firefox", $agentstring)) { $browserInfo['browser_name'] = "Firefox"; $index = strpos($agentstring, "Firefox"); $version = substr($agentstring, $index+8); $browserInfo['browser_version'] = $version; } elseif(eregi("MSIE", $agentstring)) { $browserInfo['browser_name'] = "Internet Explorer"; $index = strpos($agentstring, "MSIE"); $version = substr($agentstring, $index+5, 3); $browserInfo['browser_version'] = $version; } else { $browserInfo['browser_name'] = "Unknown"; }}function RB_osID($agentstring) { if(eregi("Windows", $agentstring)) { $browserInfo['os_name'] = "Windows"; $index = strpos($agentstring, "NT"); $version = substr($agentstring, $index+2, 4); if($version == 5.1) { $browserInfo['os_version'] = "XP"; } } else if(eregi("Linux", $agentstring)) { $browserInfo['os_name'] = "Linux"; }}function RB_infoDisplay($infoarray) { foreach($infoarray as $key => $value) { echo "$key, $value <br />"; }}[/code]my problem is as follows. when i call the methods to get the browser/OS info it seems like each method is using its own copy of the $browserInfo array. if i call the infoDisplay method, what ends up happening is nothing is displayed. now if i throw some echo statements within the Os/browser display functions then it seems like the array values are being assigned properly. no idea whats causing this but I'm sure it's a stupid scope issue. I'm kinda new to PHP and have Java programmign conventions etched into my brain so maybe it's something to do with that. MANY thanks in advance for any help received. Link to comment https://forums.phpfreaks.com/topic/34180-variable-scope-issue/ Share on other sites More sharing options...
trq Posted January 14, 2007 Share Posted January 14, 2007 The $browserInfo variable is local to each function only. If you the same variable available within each function either use the global keyword, or pass the array into the function. Link to comment https://forums.phpfreaks.com/topic/34180-variable-scope-issue/#findComment-160810 Share on other sites More sharing options...
RobinTibbs Posted January 14, 2007 Author Share Posted January 14, 2007 changed the code so now the Id functions have a parameter called $infoArray and i now call them as[code]function($USER_AGENT, $browserID)[/code]but now the array appears to be blank when i try to print contents. sorry if i am being awkward :) Link to comment https://forums.phpfreaks.com/topic/34180-variable-scope-issue/#findComment-160817 Share on other sites More sharing options...
Hypnos Posted January 14, 2007 Share Posted January 14, 2007 Seems you're use to programming languages where all vars have a global scope. PHP isn't like that. If you read this, you might understand it a bit better:http://us2.php.net/globalEven though you're bringing in $USER_AGENT, you aren't sending it back. You could say $USER_AGENT = 36; in your function, and it still wouldn't change the global $USER_AGENT.One way you can send it back is with a return on each function, and then an = statement each time you call each function. Like so:[code=php:0]$USER_AGENT = RB_browserID($USER_AGENT, $browserID);[/code]Then at the end of RB_browserID:[code=php:0]return $USER_AGENT;[/code] Link to comment https://forums.phpfreaks.com/topic/34180-variable-scope-issue/#findComment-160822 Share on other sites More sharing options...
RobinTibbs Posted January 17, 2007 Author Share Posted January 17, 2007 got this sorted now, thank you both! Link to comment https://forums.phpfreaks.com/topic/34180-variable-scope-issue/#findComment-163070 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.