0

I'm using the following code to try and print the operating system of the user:

Header:

<?php
$user_agent = getenv("HTTP_USER_AGENT");

if (strpos($user_agent, "Win") !== FALSE)
$os = "Windows"; 
else (strpos($user_agent, "Mac") !== FALSE)
$os = "Mac";
?>

Body:

<?php
if($os = "Windows")
{

}
elseif($os == "Mac")
{

} 
?>

I get the error

Parse error: syntax error, unexpected '$os' (T_VARIABLE) in C:\xampp\xamppfile\htdocs\ProjectSite\includes\identifier.php on line 7

2
  • You can't put condition in else statement. Commented Mar 11, 2014 at 11:22
  • (a) Which is line 7? (b) How do you handle the case where the $user_agent is neither Mac nor Windows? That will lead to an undefined $os, won't it? Commented Mar 11, 2014 at 11:27

2 Answers 2

1

You cannot have condition in else statement should use else if and also have practice of declaring your variable before using it,

$os = "";
if (strpos($user_agent, "Win") !== FALSE)
$os = "Windows"; 
else if(strpos($user_agent, "Mac") !== FALSE)
$os = "Mac";
Sign up to request clarification or add additional context in comments.

Comments

0
<?php
$user_agent = getenv("HTTP_USER_AGENT");

if (strpos($user_agent, "Win") !== FALSE) {
    $os = "Windows"; 

}
else if(strpos($user_agent, "Mac") !== FALSE) {
    $os = "Mac";
}

?>

1 Comment

this is not helpful unless you add some explanation of what the questioner was doing wrong and what your proposed code does

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.