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>
There are a couple of issues with your code. First
$browsers.lengthis not the way that value is calculated in PHP. It seems like.lengthis a JavaScript format? It should becount($browsers). Also, you haveSERVER['HTTP_USER_AGENT']when it should be$_SERVER['HTTP_USER_AGENT']. I also changed yourechoformat so it’s more readable. This should work without failing completely:But if I were you I would approach your logic as so using
in_arrayinstead of aforloop:Basically that
forloop is excessive for the logic presented. Just usein_arrayto 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.