-3

This is my current code, but I get a parse error when I run it? Have I just made a silly error or will this not work? Thanks in advance.

<html>
<head>
<title>PHP Test</title>
</head>
<body>
<?php 
browser();
function browser() {
    $browsers = array('chrome', 'msie', 'firefox', 'safari');
    for ($i = 0; $i < $browsers.length + 1; $i++) {
        if (SERVER['HTTP_USER_AGENT'] == $browsers[$i]) {
            echo "You are using {$browsers[$i]}.";
        }
    }
}
?>
</body>
</html>
Giacomo1968
  • 25,759
  • 11
  • 71
  • 103
  • 2
    `SERVER` should be `$_SERVER`; if there are other parse errors, it would really help if you included them in the question. – andrewsi Dec 18 '13 at 17:15
  • 1
    `SERVER` should be `$_SERVER`. – ʰᵈˑ Dec 18 '13 at 17:15
  • 2
    Don't reinvent wheels: [`get_browser()`](http://www.php.net/manual/en/function.get-browser.php) PHP often has built-ins for common web-like stuff :) – Michael Berkowski Dec 18 '13 at 17:16
  • In addition to the addition of the above comments, you could do a string match (regular expression), or even `strpos`. @andrewsi You're too quick! Beat me by 4 seconds :P – ʰᵈˑ Dec 18 '13 at 17:16
  • Question: why not just `echo "You are using {$_SERVER['HTTP_USER_AGENT']}.";`? Looping through the array seems excessive. EDIT: Or I've been beaten to suggesting that by three other users. Impressive. – zebediah49 Dec 18 '13 at 17:17
  • You should probably try var_dump($_SERVER['HTTP_USER_AGENT']); to see what that super global actually holds. – vascowhite Dec 18 '13 at 17:18

1 Answers1

1

There are a couple of issues with your code. First $browsers.length is not the way that value is calculated in PHP. It seems like .length is a JavaScript format? It should be count($browsers). Also, you have SERVER['HTTP_USER_AGENT'] when it should be $_SERVER['HTTP_USER_AGENT']. I also changed your echo format so it’s more readable. This should work without failing completely:

browser();
function browser() {
    $browsers = array('chrome', 'msie', 'firefox', 'safari');
    for ($i = 0; $i < count($browsers) + 1; $i++) {
        if ($_SERVER['HTTP_USER_AGENT'] == $browsers[$i]) {
            echo "You are using " . $browsers[$i] . ".";
        }
    }
}

But if I were you I would approach your logic as so using in_array instead of a for loop:

browser();
function browser() {
    $browsers = array('chrome', 'msie', 'firefox', 'safari');
    if (in_array($_SERVER['HTTP_USER_AGENT'], $browsers) {
        echo "You are using " . $_SERVER['HTTP_USER_AGENT'] . ".";
    }
}

Basically that for loop is excessive for the logic presented. Just use in_array to check of the value of $_SERVER['HTTP_USER_AGENT'] is actually in $browsers.

That said, browser detection is not as simple as your overall logic implies. But this at least solves your most immediate PHP problems at gives you something to build on.

Giacomo1968
  • 25,759
  • 11
  • 71
  • 103
  • 1
    `$i < count($browsers) + 1` should be `$i < count($browsers)`. Also, as none of the strings in $browsers match any user agent string, there will never be any output from this. – vascowhite Dec 18 '13 at 17:25
  • @vascowhite Yes you are right. But it’s not really the question posed. The question was why their original code was failing. And there are some glaring errors. The larger logic of their code is flawed, but at least now this will set things on the right so it doesn’t fail before it even runs. – Giacomo1968 Dec 18 '13 at 17:28
  • I think that is splitting hairs. The function still fails in that it doesn't do what it is supposed to do, it just doesn't fail visibly any more. Well, it does actually it raises "Notice: Undefined offset: 4" because you have that +1 in there. – vascowhite Dec 18 '13 at 17:29