Jump to content

variable Scope issue


RobinTibbs

Recommended Posts

writing myself a small browser/Os id system and have run into an issue. here goes

heres 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

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/global

Even 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

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.